Skip to content

Commit 032aa90

Browse files
authored
Filter undefined from binding elements with initialisers without undefined in the type (microsoft#38122)
* Filter undefined from binding elts w/o undefined-containing inits * use getTypeOfInitializer instead * improve comment based on Wesleys suggestion
1 parent ef83109 commit 032aa90

6 files changed

+22
-3
lines changed

src/compiler/checker.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7305,8 +7305,8 @@ namespace ts {
73057305
if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
73067306
parentType = getNonNullableType(parentType);
73077307
}
7308-
// Filter `undefined` from the type we check against if the parent has an initializer (which handles the `undefined` case implicitly)
7309-
else if (strictNullChecks && pattern.parent.initializer && isParameter(pattern.parent)) {
7308+
// Filter `undefined` from the type we check against if the parent has an initializer and that initializer is not possibly `undefined`
7309+
else if (strictNullChecks && pattern.parent.initializer && !(getTypeFacts(getTypeOfInitializer(pattern.parent.initializer)) & TypeFacts.EQUndefined)) {
73107310
parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined);
73117311
}
73127312

tests/baselines/reference/contextualTypeForInitalizedVariablesFiltersUndefined.errors.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
2+
tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(8,16): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
23

34

4-
==== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts (1 errors) ====
5+
==== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts (2 errors) ====
56
const fInferred = ({ a = 0 } = {}) => a;
67
// const fInferred: ({ a }?: { a?: number; }) => number
78

@@ -11,4 +12,7 @@ tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9
1112
const { s } = t;
1213
~
1314
!!! error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
15+
function fst({ s } = t) { }
16+
~
17+
!!! error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
1418

tests/baselines/reference/contextualTypeForInitalizedVariablesFiltersUndefined.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
66

77
declare var t: { s: string } | undefined;
88
const { s } = t;
9+
function fst({ s } = t) { }
910

1011

1112
//// [contextualTypeForInitalizedVariablesFiltersUndefined.js]
@@ -20,3 +21,6 @@ var fAnnotated = function (_a) {
2021
return a;
2122
};
2223
var s = t.s;
24+
function fst(_a) {
25+
var s = (_a === void 0 ? t : _a).s;
26+
}

tests/baselines/reference/contextualTypeForInitalizedVariablesFiltersUndefined.symbols

+5
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,8 @@ const { s } = t;
2020
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 7))
2121
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))
2222

23+
function fst({ s } = t) { }
24+
>fst : Symbol(fst, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 16))
25+
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 7, 14))
26+
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))
27+

tests/baselines/reference/contextualTypeForInitalizedVariablesFiltersUndefined.types

+5
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ const { s } = t;
2626
>s : any
2727
>t : { s: string; } | undefined
2828

29+
function fst({ s } = t) { }
30+
>fst : ({ s }?: { s: string; } | undefined) => void
31+
>s : any
32+
>t : { s: string; } | undefined
33+

tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
66

77
declare var t: { s: string } | undefined;
88
const { s } = t;
9+
function fst({ s } = t) { }

0 commit comments

Comments
 (0)