fix(44059): omit duplicate types (#45739)

This commit is contained in:
Oleksandr T 2021-11-06 00:36:08 +02:00 committed by GitHub
parent 2d4b243195
commit 0a628ff0c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View file

@ -1000,13 +1000,25 @@ namespace ts.codefix {
if (usage.numberIndex) {
types.push(checker.createArrayType(combineFromUsage(usage.numberIndex)));
}
if (usage.properties?.size || usage.calls?.length || usage.constructs?.length || usage.stringIndex) {
if (usage.properties?.size || usage.constructs?.length || usage.stringIndex) {
types.push(inferStructuralType(usage));
}
types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t)));
types.push(...inferNamedTypesFromProperties(usage));
const candidateTypes = (usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t));
const callsType = usage.calls?.length ? inferStructuralType(usage) : undefined;
if (callsType && candidateTypes) {
types.push(checker.getUnionType([callsType, ...candidateTypes], UnionReduction.Subtype));
}
else {
if (callsType) {
types.push(callsType);
}
if (length(candidateTypes)) {
types.push(...candidateTypes);
}
}
types.push(...inferNamedTypesFromProperties(usage));
return types;
}

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
// @noImplicitAny: false
////[|let foo;|]
////
////foo?.();
////foo = () => {}
verify.codeFix({
description: "Infer type of 'foo' from usage",
index: 0,
newRangeContent: "let foo: () => void;"
});