Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7305,8 +7305,8 @@ namespace ts {
if (strictNullChecks && declaration.flags & NodeFlags.Ambient && isParameterDeclaration(declaration)) {
parentType = getNonNullableType(parentType);
}
// Filter `undefined` from the type we check against if the parent has an initializer (which handles the `undefined` case implicitly)
else if (strictNullChecks && pattern.parent.initializer && isParameter(pattern.parent)) {
// 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 && !(getTypeFacts(getTypeOfInitializer(pattern.parent.initializer)) & TypeFacts.EQUndefined)) {
parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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(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 }?: { a?: number; }) => number

Expand All @@ -11,4 +12,7 @@ tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9
const { s } = t;
~
!!! 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'.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;

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


//// [contextualTypeForInitalizedVariablesFiltersUndefined.js]
Expand All @@ -20,3 +21,6 @@ var fAnnotated = function (_a) {
return a;
};
var s = t.s;
function fst(_a) {
var s = (_a === void 0 ? t : _a).s;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ const { s } = t;
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 7))
>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))

Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ const { s } = t;
>s : any
>t : { s: string; } | undefined

function fst({ s } = t) { }
>fst : ({ s }?: { s: string; } | undefined) => void
>s : any
>t : { s: string; } | undefined

Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;

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