microsoft / microsoft/TypeScript

Incorrectly inferred object property as string literal value

Open
#39,131 3 comments 1 reaction 1 assignee View on GitHub

@RyanCavanaugh is already working on this.

Since Jun 29, 2020.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.2

Search Terms:
incorrectly inferred string index type
inferred object property in function return type

Expected behavior:
The key values in each Variation should be inferred as string not the literal string value

Actual behavior:
The key values in each Variation are inferred as the literal string value of the first object

Related Issues:
Possibly related: https://github.com/microsoft/TypeScript/issues/38759,

Code


type Variation = {
    [testDescription: string]: () => void;
}

export type VariationProvider = {
  variations: Variation[];
};

export type VariationProviderFactory = (extras: object) => VariationProvider;

export function createVariationProvider(factory: VariationProviderFactory) {
  return factory;
}


createVariationProvider(() => {
  return {
    variations: [
      {
        foo: () => {}
      },
      {
        broken: () => {}
      },
    ]
  }
})

Output
export function createVariationProvider(factory) {
    return factory;
}
createVariationProvider(() => {
    return {
        variations: [
            {
                foo: () => { }
            },
            {
                broken: () => { }
            },
        ]
    };
});

Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.