0

I was trying to populate some test data in the database, in my Typescript / Typeorm backend project:

const createEntity = async <T extends EntityConstructor>(
  Constructor: T,
  input: Partial<InstanceType<T>>,
): Promise<InstanceType<T>> => {
  const instance = Constructor.create(input);
  return validateAndSaveEntity(instance as InstanceType<T>);
};


const seedIssues = (): Promise<Issue[]> => {

  const issues = [
    createEntity(Issue, {
      title: 'Case 1'
    }),
    createEntity(Issue, {
      title: 'Case 2'
    }),

    ...

  ];

  return Promise.all(issues);
};

The above works, but obviously I don't want to write a long long list containing the items. So I switched to using a for loop and append the items to the list:

const seedIssues = (): Promise<Issue[]> => {
  let issues = []
  for (let i = 0; i < 100; i++){
    issues.push(
      createEntity(Issue, {
        title: 'Case ' + String(i),
      })
    )
  }

  return Promise.all(issues);
};

However, this code does not work and it throws this error:

src/database/createGuestAccount2.ts:172:7 - error TS2345: Argument of type 'Promise<Issue>' is not assignable to parameter of type 'never'.

172       createEntity(Issue, {
          ~~~~~~~~~~~~~~~~~~~~~
173         title: 'Case ' + String(i),
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
181       })
    ~~~~~~~~

    at createTSError (/Users/api/node_modules/ts-node/src/index.ts:293:12)
    at reportTSError (/Users/api/node_modules/ts-node/src/index.ts:297:19)
    at getOutput (/Users/api/node_modules/ts-node/src/index.ts:399:34)
    at Object.compile (/Users/api/node_modules/ts-node/src/index.ts:457:32)
    at Module.m._compile (/Users/api/node_modules/ts-node/src/index.ts:530:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/api/node_modules/ts-node/src/index.ts:533:12)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)

What does it mean? How can I proceed to create many test data points with the for loop?

Kid_Learning_C
  • 2,605
  • 4
  • 39
  • 71
  • _Where_ is that error? If it's on the push, did you try specifying the type of the issues array? – jonrsharpe Aug 18 '21 at 08:54
  • Does this answer your question? [What is "not assignable to parameter of type never" error in typescript?](https://stackoverflow.com/questions/52423842/what-is-not-assignable-to-parameter-of-type-never-error-in-typescript) – A_A Aug 18 '21 at 09:04

1 Answers1

1

Give the issues variable a type when you declare it:

const seedIssues = (): Promise<Issue[]> => {
  let issues: Promise<Issue>[] = []
  for (let i = 0; i < 100; i++){
    issues.push(
      createEntity(Issue, {
        title: 'Case ' + String(i),
      })
    )
  }

  return Promise.all(issues);
};
A_A
  • 1,832
  • 2
  • 11
  • 17