Filter undefined from binding elements with initialisers without undefined in the type (#38122)

* Filter undefined from binding elts w/o undefined-containing inits

* use getTypeOfInitializer instead

* improve comment based on Wesleys suggestion
This commit is contained in:
Nathan Shively-Sanders 2020-04-22 15:45:15 -07:00 committed by GitHub
parent ef83109dbf
commit 032aa90289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 3 deletions

View file

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

View file

@ -1,7 +1,8 @@
tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'. tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(8,16): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
==== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts (1 errors) ==== ==== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts (2 errors) ====
const fInferred = ({ a = 0 } = {}) => a; const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number // const fInferred: ({ a }?: { a?: number; }) => number
@ -11,4 +12,7 @@ tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9
const { s } = t; const { s } = t;
~ ~
!!! error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'. !!! error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.
function fst({ s } = t) { }
~
!!! error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.

View file

@ -6,6 +6,7 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
declare var t: { s: string } | undefined; declare var t: { s: string } | undefined;
const { s } = t; const { s } = t;
function fst({ s } = t) { }
//// [contextualTypeForInitalizedVariablesFiltersUndefined.js] //// [contextualTypeForInitalizedVariablesFiltersUndefined.js]
@ -20,3 +21,6 @@ var fAnnotated = function (_a) {
return a; return a;
}; };
var s = t.s; var s = t.s;
function fst(_a) {
var s = (_a === void 0 ? t : _a).s;
}

View file

@ -20,3 +20,8 @@ const { s } = t;
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 7)) >s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 7))
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11)) >t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))
function fst({ s } = t) { }
>fst : Symbol(fst, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 16))
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 7, 14))
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))

View file

@ -26,3 +26,8 @@ const { s } = t;
>s : any >s : any
>t : { s: string; } | undefined >t : { s: string; } | undefined
function fst({ s } = t) { }
>fst : ({ s }?: { s: string; } | undefined) => void
>s : any
>t : { s: string; } | undefined

View file

@ -6,3 +6,4 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
declare var t: { s: string } | undefined; declare var t: { s: string } | undefined;
const { s } = t; const { s } = t;
function fst({ s } = t) { }