microsoft / microsoft/TypeScript

Incorrectly inferred object property as string literal value

Ouverte
#39,131 3 commentaires 1 réaction 1 personne assignée Voir sur GitHub

@RyanCavanaugh y travaille déjà.

Depuis le 29/6/2020.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.