Skip to content

Filter undefined only in binding patterns in parameters w/initialiser #38116

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 1 commit 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
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7306,7 +7306,7 @@ namespace ts {
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) {
else if (strictNullChecks && pattern.parent.initializer && isParameter(pattern.parent)) {
parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts(7,9): error TS2339: Property 's' does not exist on type '{ s: string; } | undefined'.


==== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts (1 errors) ====
const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number

const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;

declare var t: { s: string } | undefined;
const { 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 @@ -2,7 +2,11 @@
const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number

const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;

declare var t: { s: string } | undefined;
const { s } = t;


//// [contextualTypeForInitalizedVariablesFiltersUndefined.js]
"use strict";
Expand All @@ -15,3 +19,4 @@ var fAnnotated = function (_a) {
var _b = (_a === void 0 ? {} : _a).a, a = _b === void 0 ? 0 : _b;
return a;
};
var s = t.s;
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 3, 39))
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 3, 39))

declare var t: { s: string } | undefined;
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 16))

const { s } = t;
>s : Symbol(s, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 6, 7))
>t : Symbol(t, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 5, 11))

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

declare var t: { s: string } | undefined;
>t : { s: string; } | undefined
>s : string

const { s } = t;
>s : any
>t : { s: string; } | undefined

Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number

const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;

declare var t: { s: string } | undefined;
const { s } = t;