Skip to content

Commit 0a628ff

Browse files
authored
fix(44059): omit duplicate types (microsoft#45739)
1 parent 2d4b243 commit 0a628ff

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/services/codefixes/inferFromUsage.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,13 +1000,25 @@ namespace ts.codefix {
10001000
if (usage.numberIndex) {
10011001
types.push(checker.createArrayType(combineFromUsage(usage.numberIndex)));
10021002
}
1003-
if (usage.properties?.size || usage.calls?.length || usage.constructs?.length || usage.stringIndex) {
1003+
if (usage.properties?.size || usage.constructs?.length || usage.stringIndex) {
10041004
types.push(inferStructuralType(usage));
10051005
}
10061006

1007-
types.push(...(usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t)));
1008-
types.push(...inferNamedTypesFromProperties(usage));
1007+
const candidateTypes = (usage.candidateTypes || []).map(t => checker.getBaseTypeOfLiteralType(t));
1008+
const callsType = usage.calls?.length ? inferStructuralType(usage) : undefined;
1009+
if (callsType && candidateTypes) {
1010+
types.push(checker.getUnionType([callsType, ...candidateTypes], UnionReduction.Subtype));
1011+
}
1012+
else {
1013+
if (callsType) {
1014+
types.push(callsType);
1015+
}
1016+
if (length(candidateTypes)) {
1017+
types.push(...candidateTypes);
1018+
}
1019+
}
10091020

1021+
types.push(...inferNamedTypesFromProperties(usage));
10101022
return types;
10111023
}
10121024

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @noImplicitAny: false
4+
////[|let foo;|]
5+
////
6+
////foo?.();
7+
////foo = () => {}
8+
9+
verify.codeFix({
10+
description: "Infer type of 'foo' from usage",
11+
index: 0,
12+
newRangeContent: "let foo: () => void;"
13+
});

0 commit comments

Comments
 (0)