Skip to content

Commit 3407b6b

Browse files
committed
Remove redundant removeMissingType calls
1 parent 31d5bf7 commit 3407b6b

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

src/compiler/checker.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6982,7 +6982,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
69826982
}
69836983

69846984
function typeReferenceToTypeNode(type: TypeReference) {
6985-
let typeArguments: readonly Type[] = getTypeArguments(type);
6985+
const typeArguments = getTypeArguments(type);
69866986
if (type.target === globalArrayType || type.target === globalReadonlyArrayType) {
69876987
if (context.flags & NodeBuilderFlags.WriteArrayAsGenericType) {
69886988
const typeArgumentNode = typeToTypeNodeHelper(typeArguments[0], context);
@@ -6993,7 +6993,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
69936993
return type.target === globalArrayType ? arrayType : factory.createTypeOperatorNode(SyntaxKind.ReadonlyKeyword, arrayType);
69946994
}
69956995
else if (type.target.objectFlags & ObjectFlags.Tuple) {
6996-
typeArguments = sameMap(typeArguments, (t, i) => removeMissingType(t, !!((type.target as TupleType).elementFlags[i] & ElementFlags.Optional)));
69976996
if (typeArguments.length > 0) {
69986997
const arity = getTypeReferenceArity(type);
69996998
const tupleConstituentNodes = mapToTypeNodes(typeArguments.slice(0, arity), context);
@@ -19928,11 +19927,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1992819927
let reportedError = false;
1992919928
for (const value of iterator) {
1993019929
const { errorNode: prop, innerExpression: next, nameType, errorMessage } = value;
19931-
let targetPropType = getBestMatchIndexedAccessTypeOrUndefined(source, target, nameType);
19930+
const targetPropType = getBestMatchIndexedAccessTypeOrUndefined(source, target, nameType);
1993219931
if (!targetPropType || targetPropType.flags & TypeFlags.IndexedAccess) continue; // Don't elaborate on indexes on generic variables
19933-
let sourcePropType = getIndexedAccessTypeOrUndefined(source, nameType);
19932+
const sourcePropType = getIndexedAccessTypeOrUndefined(source, nameType);
1993419933
if (!sourcePropType) continue;
19935-
const propName = getPropertyNameFromIndex(nameType, /*accessNode*/ undefined);
1993619934
if (!checkTypeRelatedTo(sourcePropType, targetPropType, relation, /*errorNode*/ undefined)) {
1993719935
const elaborated = next && elaborateError(next, sourcePropType, targetPropType, relation, /*headMessage*/ undefined, containingMessageChain, errorOutputContainer);
1993819936
reportedError = true;
@@ -19947,10 +19945,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
1994719945
resultObj.errors = [diag];
1994819946
}
1994919947
else {
19950-
const targetIsOptional = !!(propName && (getPropertyOfType(target, propName) || unknownSymbol).flags & SymbolFlags.Optional);
19951-
const sourceIsOptional = !!(propName && (getPropertyOfType(source, propName) || unknownSymbol).flags & SymbolFlags.Optional);
19952-
targetPropType = removeMissingType(targetPropType, targetIsOptional);
19953-
sourcePropType = removeMissingType(sourcePropType, targetIsOptional && sourceIsOptional);
1995419948
const result = checkTypeRelatedTo(specificSource, targetPropType, relation, prop, errorMessage, containingMessageChain, resultObj);
1995519949
if (result && specificSource !== sourcePropType) {
1995619950
// If for whatever reason the expression type doesn't yield an error, make sure we still issue an error on the sourcePropType
@@ -29832,7 +29826,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2983229826
// If index is before any spread element and within the fixed part of the contextual tuple type, return
2983329827
// the type of the contextual tuple element.
2983429828
if ((firstSpreadIndex === undefined || index < firstSpreadIndex) && index < t.target.fixedLength) {
29835-
return removeMissingType(getTypeArguments(t)[index], !!(t.target.elementFlags[index] && ElementFlags.Optional));
29829+
return getTypeArguments(t)[index];
2983629830
}
2983729831
// When the length is known and the index is after all spread elements we compute the offset from the element
2983829832
// to the end and the number of ending fixed elements in the contextual tuple type.
@@ -30534,7 +30528,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3053430528
const inConstContext = isConstContext(node);
3053530529
const contextualType = getApparentTypeOfContextualType(node, /*contextFlags*/ undefined);
3053630530
const inTupleContext = isSpreadIntoCallOrNew(node) || !!contextualType && someType(contextualType, isTupleLikeType);
30537-
let hasOmittedExpression = false;
3053830531
for (let i = 0; i < elementCount; i++) {
3053930532
const e = elements[i];
3054030533
if (e.kind === SyntaxKind.SpreadElement) {
@@ -30571,14 +30564,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
3057130564
}
3057230565
}
3057330566
else if (exactOptionalPropertyTypes && e.kind === SyntaxKind.OmittedExpression) {
30574-
hasOmittedExpression = true;
30575-
elementTypes.push(undefinedOrMissingType);
30576-
elementFlags.push(ElementFlags.Optional);
30567+
elementTypes.push(undefinedType);
30568+
elementFlags.push(ElementFlags.Required);
3057730569
}
3057830570
else {
3057930571
const type = checkExpressionForMutableLocation(e, checkMode, forceTuple);
30580-
elementTypes.push(addOptionality(type, /*isProperty*/ true, hasOmittedExpression));
30581-
elementFlags.push(hasOmittedExpression ? ElementFlags.Optional : ElementFlags.Required);
30572+
elementTypes.push(type);
30573+
elementFlags.push(ElementFlags.Required);
3058230574
if (inTupleContext && checkMode && checkMode & CheckMode.Inferential && !(checkMode & CheckMode.SkipContextSensitive) && isContextSensitive(e)) {
3058330575
const inferenceContext = getInferenceContext(node);
3058430576
Debug.assert(inferenceContext); // In CheckMode.Inferential we should always have an inference context

tests/baselines/reference/strictOptionalProperties1.errors.txt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,28 @@ strictOptionalProperties1.ts(53,5): error TS2412: Type 'undefined' is not assign
77
strictOptionalProperties1.ts(60,5): error TS2322: Type 'undefined' is not assignable to type 'string'.
88
strictOptionalProperties1.ts(64,5): error TS2322: Type '[number, string?, string?]' is not assignable to type '[number, string?]'.
99
Target allows only 2 element(s) but source may have more.
10-
strictOptionalProperties1.ts(71,5): error TS2322: Type '[number, never?]' is not assignable to type '[number, string?, boolean?]'.
10+
strictOptionalProperties1.ts(71,5): error TS2322: Type '[number, undefined]' is not assignable to type '[number, string?, boolean?]'.
1111
Type at position 1 in source is not compatible with type at position 1 in target.
1212
Type 'undefined' is not assignable to type 'string'.
13-
strictOptionalProperties1.ts(72,5): error TS2322: Type '[number, never?, never?]' is not assignable to type '[number, string?, boolean?]'.
13+
strictOptionalProperties1.ts(72,5): error TS2322: Type '[number, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
1414
Type at position 1 in source is not compatible with type at position 1 in target.
1515
Type 'undefined' is not assignable to type 'string'.
16-
strictOptionalProperties1.ts(73,5): error TS2322: Type '[number, never?, never?, never?]' is not assignable to type '[number, string?, boolean?]'.
17-
Target allows only 3 element(s) but source may have more.
18-
strictOptionalProperties1.ts(74,5): error TS2322: Type '[never?, never?, true?]' is not assignable to type '[number, string?, boolean?]'.
19-
Source provides no match for required element at position 0 in target.
16+
strictOptionalProperties1.ts(73,5): error TS2322: Type '[number, undefined, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
17+
Source has 4 element(s) but target allows only 3.
18+
strictOptionalProperties1.ts(74,5): error TS2322: Type '[undefined, undefined, true]' is not assignable to type '[number, string?, boolean?]'.
19+
Type at position 0 in source is not compatible with type at position 0 in target.
20+
Type 'undefined' is not assignable to type 'number'.
2021
strictOptionalProperties1.ts(75,5): error TS2322: Type '[number, undefined, true]' is not assignable to type '[number, string?, boolean?]'.
2122
Type at position 1 in source is not compatible with type at position 1 in target.
2223
Type 'undefined' is not assignable to type 'string'.
2324
strictOptionalProperties1.ts(99,7): error TS2375: Type '{ foo: undefined; bar: string; }' is not assignable to type 'InputProps' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
2425
Types of property 'foo' are incompatible.
2526
Type 'undefined' is not assignable to type 'string'.
2627
strictOptionalProperties1.ts(105,7): error TS2322: Type '[number, undefined]' is not assignable to type '[number, string?, boolean?]'.
27-
Type at position 1 in source is not compatible with type at position 1 in target.
28-
Type 'undefined' is not assignable to type 'string'.
2928
strictOptionalProperties1.ts(106,7): error TS2322: Type '[number, string, undefined]' is not assignable to type '[number, string?, boolean?]'.
3029
Type at position 2 in source is not compatible with type at position 2 in target.
3130
Type 'undefined' is not assignable to type 'boolean'.
3231
strictOptionalProperties1.ts(107,7): error TS2322: Type '[number, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
33-
Type at position 1 in source is not compatible with type at position 1 in target.
34-
Type 'undefined' is not assignable to type 'string'.
3532
strictOptionalProperties1.ts(111,7): error TS2375: Type '{ foo: undefined; }' is not assignable to type '{ foo?: number; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
3633
Types of property 'foo' are incompatible.
3734
Type 'undefined' is not assignable to type 'number'.
@@ -142,22 +139,23 @@ strictOptionalProperties1.ts(211,1): error TS2322: Type 'string | boolean | unde
142139
t = [42, 'abc', true];
143140
t = [42, ,];
144141
~
145-
!!! error TS2322: Type '[number, never?]' is not assignable to type '[number, string?, boolean?]'.
142+
!!! error TS2322: Type '[number, undefined]' is not assignable to type '[number, string?, boolean?]'.
146143
!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target.
147144
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
148145
t = [42, , ,];
149146
~
150-
!!! error TS2322: Type '[number, never?, never?]' is not assignable to type '[number, string?, boolean?]'.
147+
!!! error TS2322: Type '[number, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
151148
!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target.
152149
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
153150
t = [42, , , ,]; // Error
154151
~
155-
!!! error TS2322: Type '[number, never?, never?, never?]' is not assignable to type '[number, string?, boolean?]'.
156-
!!! error TS2322: Target allows only 3 element(s) but source may have more.
152+
!!! error TS2322: Type '[number, undefined, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
153+
!!! error TS2322: Source has 4 element(s) but target allows only 3.
157154
t = [, , true]; // Error
158155
~
159-
!!! error TS2322: Type '[never?, never?, true?]' is not assignable to type '[number, string?, boolean?]'.
160-
!!! error TS2322: Source provides no match for required element at position 0 in target.
156+
!!! error TS2322: Type '[undefined, undefined, true]' is not assignable to type '[number, string?, boolean?]'.
157+
!!! error TS2322: Type at position 0 in source is not compatible with type at position 0 in target.
158+
!!! error TS2322: Type 'undefined' is not assignable to type 'number'.
161159
t = [42, undefined, true]; // Error
162160
~
163161
!!! error TS2322: Type '[number, undefined, true]' is not assignable to type '[number, string?, boolean?]'.
@@ -199,8 +197,6 @@ strictOptionalProperties1.ts(211,1): error TS2322: Type 'string | boolean | unde
199197
const t2: [number, string?, boolean?] = [1, undefined];
200198
~~
201199
!!! error TS2322: Type '[number, undefined]' is not assignable to type '[number, string?, boolean?]'.
202-
!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target.
203-
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
204200
const t3: [number, string?, boolean?] = [1, "string", undefined];
205201
~~
206202
!!! error TS2322: Type '[number, string, undefined]' is not assignable to type '[number, string?, boolean?]'.
@@ -209,8 +205,6 @@ strictOptionalProperties1.ts(211,1): error TS2322: Type 'string | boolean | unde
209205
const t4: [number, string?, boolean?] = [1, undefined, undefined];
210206
~~
211207
!!! error TS2322: Type '[number, undefined, undefined]' is not assignable to type '[number, string?, boolean?]'.
212-
!!! error TS2322: Type at position 1 in source is not compatible with type at position 1 in target.
213-
!!! error TS2322: Type 'undefined' is not assignable to type 'string'.
214208

215209
// Example from #13195
216210

tests/baselines/reference/strictOptionalProperties1.types

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -328,33 +328,33 @@ function f5(t: [number, string?, boolean?]) {
328328
>true : true
329329

330330
t = [42, ,];
331-
>t = [42, ,] : [number, never?]
331+
>t = [42, ,] : [number, undefined]
332332
>t : [number, string?, boolean?]
333-
>[42, ,] : [number, never?]
333+
>[42, ,] : [number, undefined]
334334
>42 : 42
335335
> : undefined
336336

337337
t = [42, , ,];
338-
>t = [42, , ,] : [number, never?, never?]
338+
>t = [42, , ,] : [number, undefined, undefined]
339339
>t : [number, string?, boolean?]
340-
>[42, , ,] : [number, never?, never?]
340+
>[42, , ,] : [number, undefined, undefined]
341341
>42 : 42
342342
> : undefined
343343
> : undefined
344344

345345
t = [42, , , ,]; // Error
346-
>t = [42, , , ,] : [number, never?, never?, never?]
346+
>t = [42, , , ,] : [number, undefined, undefined, undefined]
347347
>t : [number, string?, boolean?]
348-
>[42, , , ,] : [number, never?, never?, never?]
348+
>[42, , , ,] : [number, undefined, undefined, undefined]
349349
>42 : 42
350350
> : undefined
351351
> : undefined
352352
> : undefined
353353

354354
t = [, , true]; // Error
355-
>t = [, , true] : [never?, never?, true?]
355+
>t = [, , true] : [undefined, undefined, true]
356356
>t : [number, string?, boolean?]
357-
>[, , true] : [never?, never?, true?]
357+
>[, , true] : [undefined, undefined, true]
358358
> : undefined
359359
> : undefined
360360
>true : true
@@ -379,34 +379,34 @@ function f6() {
379379
>2 : 2
380380

381381
const t2 = [1, 2, ,] as const;
382-
>t2 : readonly [1, 2, never?]
383-
>[1, 2, ,] as const : readonly [1, 2, never?]
384-
>[1, 2, ,] : readonly [1, 2, never?]
382+
>t2 : readonly [1, 2, undefined]
383+
>[1, 2, ,] as const : readonly [1, 2, undefined]
384+
>[1, 2, ,] : readonly [1, 2, undefined]
385385
>1 : 1
386386
>2 : 2
387387
> : undefined
388388

389389
const t3 = [1, 2, , ,] as const;
390-
>t3 : readonly [1, 2, never?, never?]
391-
>[1, 2, , ,] as const : readonly [1, 2, never?, never?]
392-
>[1, 2, , ,] : readonly [1, 2, never?, never?]
390+
>t3 : readonly [1, 2, undefined, undefined]
391+
>[1, 2, , ,] as const : readonly [1, 2, undefined, undefined]
392+
>[1, 2, , ,] : readonly [1, 2, undefined, undefined]
393393
>1 : 1
394394
>2 : 2
395395
> : undefined
396396
> : undefined
397397

398398
const t4 = [1, , 2] as const;
399-
>t4 : readonly [1, never?, 2?]
400-
>[1, , 2] as const : readonly [1, never?, 2?]
401-
>[1, , 2] : readonly [1, never?, 2?]
399+
>t4 : readonly [1, undefined, 2]
400+
>[1, , 2] as const : readonly [1, undefined, 2]
401+
>[1, , 2] : readonly [1, undefined, 2]
402402
>1 : 1
403403
> : undefined
404404
>2 : 2
405405

406406
const t5 = [1, , , 2] as const;
407-
>t5 : readonly [1, never?, never?, 2?]
408-
>[1, , , 2] as const : readonly [1, never?, never?, 2?]
409-
>[1, , , 2] : readonly [1, never?, never?, 2?]
407+
>t5 : readonly [1, undefined, undefined, 2]
408+
>[1, , , 2] as const : readonly [1, undefined, undefined, 2]
409+
>[1, , , 2] : readonly [1, undefined, undefined, 2]
410410
>1 : 1
411411
> : undefined
412412
> : undefined

0 commit comments

Comments
 (0)