Skip to content

Filter the type of a binding pattern to not include undefined is the pattern parent has an initializer #37309

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
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: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7154,6 +7154,10 @@ 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) {
parentType = getTypeWithFacts(parentType, TypeFacts.NEUndefined);
}

let type: Type | undefined;
if (pattern.kind === SyntaxKind.ObjectBindingPattern) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [contextualTypeForInitalizedVariablesFiltersUndefined.ts]
const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number

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

//// [contextualTypeForInitalizedVariablesFiltersUndefined.js]
"use strict";
var fInferred = function (_a) {
var _b = (_a === void 0 ? {} : _a).a, a = _b === void 0 ? 0 : _b;
return a;
};
// const fInferred: ({ a }?: { a?: number; }) => number
var fAnnotated = function (_a) {
var _b = (_a === void 0 ? {} : _a).a, a = _b === void 0 ? 0 : _b;
return a;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts ===
const fInferred = ({ a = 0 } = {}) => a;
>fInferred : Symbol(fInferred, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 0, 5))
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 0, 20))
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 0, 20))

// const fInferred: ({ a }?: { a?: number; }) => number

const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
>fAnnotated : Symbol(fAnnotated, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 3, 5))
>fInferred : Symbol(fInferred, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 0, 5))
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 3, 39))
>a : Symbol(a, Decl(contextualTypeForInitalizedVariablesFiltersUndefined.ts, 3, 39))

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/contextualTypeForInitalizedVariablesFiltersUndefined.ts ===
const fInferred = ({ a = 0 } = {}) => a;
>fInferred : ({ a }?: { a?: number | undefined; }) => number
>({ a = 0 } = {}) => a : ({ a }?: { a?: number | undefined; }) => number
>a : number
>0 : 0
>{} : { a?: number | undefined; }
>a : number

// const fInferred: ({ a }?: { a?: number; }) => number

const fAnnotated: typeof fInferred = ({ a = 0 } = {}) => a;
>fAnnotated : ({ a }?: { a?: number | undefined; }) => number
>fInferred : ({ a }?: { a?: number | undefined; }) => number
>({ a = 0 } = {}) => a : ({ a }?: { a?: number | undefined; } | undefined) => number
>a : number
>0 : 0
>{} : {}
>a : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @strict: true
const fInferred = ({ a = 0 } = {}) => a;
// const fInferred: ({ a }?: { a?: number; }) => number

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