microsoft / microsoft/TypeScript
Conditional + annotation causes erroneous typing
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.3k
- Merge moyen
- 2 j 4 h
- PR mergées (30 j)
- 132
Description
TypeScript Version: 33.9.4 & 4.0.0@beta
Search Terms: Type annotation wrongly overrides generic with errors
Code
const createMatrix = <D extends number, T>(
dimensions: D,
initialValues: T | null = null
): Matrix<D, T> => {
const currentDimensionLength = dimensions;
const remainingDimensions = dimensions - 1;
const needsRecursion = remainingDimensions > 0;
const currentMatrix = Array(currentDimensionLength).fill(initialValues);
const finalMatrix = needsRecursion
? currentMatrix.map(() =>
createMatrix(remainingDimensions, initialValues)
)
: currentMatrix;
return finalMatrix as Matrix<D, T>;
};
type Matrix<D extends number, T> = D extends 1
? T[]
: D extends 2
? T[][]
: D extends 3
? T[][][]
: any[][][][];
Expected behavior:
Adding type annotations to variables should be compatible with the functions return signature, causing no type errors
Actual behavior:
Assigning an annotated variable to the return value of the function causes an error. The generic T is wrongly inferred to be any of array where depth < D where it should be inferred to only the base type of the nested array. Removing the annotation solves the problem but removes type safety when variable assignment happens after creation.
The problem seems to be that (when annotated) the type is inferred to be the union of each type in Matrix that's preceding the conditional that returns true.
const n1: number[] = createMatrix(1); // <- works - generic T is set to `number`
const n2: number[][] = createMatrix(2); // <- doesn't work - generic T is set to `number | number[]`
const n3: number[][][] = createMatrix(3); // <- doesn't work - generic T is set to `number | number[] | number[][]`
const n4 = createMatrix(2); // <- regular inference works - generic T is `unknown`, return type is unknown[][]
const n5: number[][] = createMatrix(2, 0); // <- works when - initialValues is set - generic T is `number`, return type is number[][]
const n6: unknown[][] = createMatrix(2); // <- works fine, as type T is unknown. A more specific annotation should constrain `unknown` to `number`
Playground Link: Link
Related Issues:
None
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Utilisez la reproduction Playground liée avec TypeScript 3.9.4 et 4.0.0-beta, en vous concentrant sur les appels n2 et n3 annotés par rapport aux cas non annotés et explicitement initialisés. Suivez l’inférence de types et le comportement du type de retour conditionnel jusqu’à identifier la source de l’union accumulée ; le travail est terminé lorsque les appels annotés produisent les types de tableaux imbriqués attendus sans erreurs.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100