-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Fixes #17080 #21328
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
Fixes #17080 #21328
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tests/baselines/reference/optionalParameterInDestructuringWithInitializer.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(6,8): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
Type 'undefined' is not assignable to type 'number'. | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(16,7): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
Type 'undefined' is not assignable to type 'number'. | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(21,7): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
Type 'undefined' is not assignable to type 'number'. | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(31,8): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
Type 'undefined' is not assignable to type 'number'. | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(45,10): error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
Type 'undefined' is not assignable to type 'number'. | ||
tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts(53,11): error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'. | ||
Type 'null' is not assignable to type 'number | undefined'. | ||
|
||
|
||
==== tests/cases/compiler/optionalParameterInDestructuringWithInitializer.ts (6 errors) ==== | ||
// https://github.com/Microsoft/TypeScript/issues/17080 | ||
|
||
declare function f(a:number,b:number): void; | ||
|
||
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) { | ||
f(a, b) | ||
~ | ||
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
!!! error TS2345: Type 'undefined' is not assignable to type 'number'. | ||
// error | ||
} | ||
|
||
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) { | ||
f(a, b) | ||
// no error | ||
} | ||
|
||
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) { | ||
f(a,b) | ||
~ | ||
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
!!! error TS2345: Type 'undefined' is not assignable to type 'number'. | ||
// error | ||
} | ||
|
||
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) { | ||
f(b,c) | ||
~ | ||
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
!!! error TS2345: Type 'undefined' is not assignable to type 'number'. | ||
// error | ||
} | ||
|
||
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) { | ||
f(b, c) | ||
// no error | ||
} | ||
|
||
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) { | ||
f(b, c) | ||
~ | ||
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
!!! error TS2345: Type 'undefined' is not assignable to type 'number'. | ||
// error | ||
} | ||
|
||
function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1, c: 2}, d: 3} ) { | ||
f(b, c) | ||
// no error | ||
} | ||
|
||
interface Foo { | ||
readonly bar?: number; | ||
} | ||
|
||
function performFoo({ bar }: Foo = {}) { | ||
useBar(bar); | ||
~~~ | ||
!!! error TS2345: Argument of type 'number | undefined' is not assignable to parameter of type 'number'. | ||
!!! error TS2345: Type 'undefined' is not assignable to type 'number'. | ||
} | ||
|
||
declare function useBar(bar: number): void; | ||
|
||
performFoo(); | ||
|
||
function performFoo2({ bar = null }: Foo = {}) { | ||
useBar2(bar); | ||
~~~ | ||
!!! error TS2345: Argument of type 'number | null' is not assignable to parameter of type 'number | undefined'. | ||
!!! error TS2345: Type 'null' is not assignable to type 'number | undefined'. | ||
} | ||
|
||
declare function useBar2(bar: number | undefined): void; | ||
|
||
performFoo2(); | ||
|
108 changes: 108 additions & 0 deletions
108
tests/baselines/reference/optionalParameterInDestructuringWithInitializer.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
//// [optionalParameterInDestructuringWithInitializer.ts] | ||
// https://github.com/Microsoft/TypeScript/issues/17080 | ||
|
||
declare function f(a:number,b:number): void; | ||
|
||
function func1( {a, b}: {a: number, b?: number} = {a: 1, b: 2} ) { | ||
f(a, b) | ||
// error | ||
} | ||
|
||
function func2( {a, b = 3}: {a: number, b?:number} = {a: 1,b: 2} ) { | ||
f(a, b) | ||
// no error | ||
} | ||
|
||
function func3( {a, b}: {a: number, b?: number} = {a: 1} ) { | ||
f(a,b) | ||
// error | ||
} | ||
|
||
function func4( {a: {b, c}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) { | ||
f(b,c) | ||
// error | ||
} | ||
|
||
function func5({a: {b, c = 4}, d}: {a: {b: number,c?: number},d: number} = {a: {b: 1,c: 2},d: 3} ) { | ||
f(b, c) | ||
// no error | ||
} | ||
|
||
function func6( {a: {b, c} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1,c: 2}, d: 3} ) { | ||
f(b, c) | ||
// error | ||
} | ||
|
||
function func7( {a: {b, c = 6} = {b: 4, c: 5}, d}: {a: {b: number, c?: number}, d: number} = {a: {b: 1, c: 2}, d: 3} ) { | ||
f(b, c) | ||
// no error | ||
} | ||
|
||
interface Foo { | ||
readonly bar?: number; | ||
} | ||
|
||
function performFoo({ bar }: Foo = {}) { | ||
useBar(bar); | ||
} | ||
|
||
declare function useBar(bar: number): void; | ||
|
||
performFoo(); | ||
|
||
function performFoo2({ bar = null }: Foo = {}) { | ||
useBar2(bar); | ||
} | ||
|
||
declare function useBar2(bar: number | undefined): void; | ||
|
||
performFoo2(); | ||
|
||
|
||
//// [optionalParameterInDestructuringWithInitializer.js] | ||
// https://github.com/Microsoft/TypeScript/issues/17080 | ||
function func1(_a) { | ||
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, b = _b.b; | ||
f(a, b); | ||
// error | ||
} | ||
function func2(_a) { | ||
var _b = _a === void 0 ? { a: 1, b: 2 } : _a, a = _b.a, _c = _b.b, b = _c === void 0 ? 3 : _c; | ||
f(a, b); | ||
// no error | ||
} | ||
function func3(_a) { | ||
var _b = _a === void 0 ? { a: 1 } : _a, a = _b.a, b = _b.b; | ||
f(a, b); | ||
// error | ||
} | ||
function func4(_a) { | ||
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, b = _c.b, c = _c.c, d = _b.d; | ||
f(b, c); | ||
// error | ||
} | ||
function func5(_a) { | ||
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, b = _c.b, _d = _c.c, c = _d === void 0 ? 4 : _d, d = _b.d; | ||
f(b, c); | ||
// no error | ||
} | ||
function func6(_a) { | ||
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, _d = _c === void 0 ? { b: 4, c: 5 } : _c, b = _d.b, c = _d.c, d = _b.d; | ||
f(b, c); | ||
// error | ||
} | ||
function func7(_a) { | ||
var _b = _a === void 0 ? { a: { b: 1, c: 2 }, d: 3 } : _a, _c = _b.a, _d = _c === void 0 ? { b: 4, c: 5 } : _c, b = _d.b, _e = _d.c, c = _e === void 0 ? 6 : _e, d = _b.d; | ||
f(b, c); | ||
// no error | ||
} | ||
function performFoo(_a) { | ||
var bar = (_a === void 0 ? {} : _a).bar; | ||
useBar(bar); | ||
} | ||
performFoo(); | ||
function performFoo2(_a) { | ||
var _b = (_a === void 0 ? {} : _a).bar, bar = _b === void 0 ? null : _b; | ||
useBar2(bar); | ||
} | ||
performFoo2(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any idea why getRootDeclaration was here? Was this copied from some other source that would also have an erroneous call to it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tracked it back to #14498, which switched to the current narrowing approach that this PR fixes. From my memory of it, I think that I inserted
getRootDeclaration
during development, then forgot to remove it after changing other parts of the code. Then none of our test cases caught the bug.