Skip to content

Typescript: randEmail, randFirstName, and randLastName (and likely more) return type string[] when no options presentΒ #74

@kpervin

Description

@kpervin

Is this a regression?

No

Description

Expected Behavior:

The following

const fields = {
    contactEmail: randEmail(),
    contactFirstName: randFirstName(),
    contactLastName: randLastName(),
  };

should report return values of

const fields = {
    contactEmail: string,
    contactFirstName: string,
    contactLastName: string
  };

Current behavior:

The above functions result in reporting

const fields = {
    contactEmail: string[],
    contactFirstName: string[],
    contactLastName: string[]
  };

Suggested fix:

declare function rand*<Options extends FakeOptions = undefined>(options?: Options): Options["length"] extends number ? string[] : string;

instead of

declare function rand*<Options extends FakeOptions>(options?: Options): Options["length"] extends number ? string[] : string;

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

The issue simply has to do with that the type seems to favor the left-hand side of the type argument.

Please provide the environment you discovered this bug in

tsconfig: {
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
    "esModuleInterop": true
  }
}

Webstorm 2021.3
VSCode 1.63.2

Anything else?

For some reason, the error does not present itself when run in Code Sandbox with the same tsconfig. However, it has presented itself in two IDE with the above tsconfig.

Do you want to create a pull request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions