-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Assignability should check an index signature or rest type of the source against optional properties of the target. #27591
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3b5caa3
Assignability should check an index signature or rest type of the source
mattmccutchen b3a22ac
Merge branch 'master' into issue-27144
sandersn 20d7b20
Merge branch 'master' into issue-27144
sandersn e4e34f8
Merge branch 'master' into issue-27144
sandersn f889cc5
Merge remote-tracking branch 'upstream/master' into issue-27144
mattmccutchen bdfb193
Style change
mattmccutchen 4534335
Merge branch 'master' into issue-27144
sandersn afac48c
do not handle private identifiers
sandersn 5fa6dfd
check for undefined valueDeclaratio too
sandersn e428ed6
quick fix for API test failure
sandersn 133968c
cover up another error
sandersn 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/assignIndexSignatureToOptionalProperty.errors.txt
This file contains 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,30 @@ | ||
tests/cases/compiler/assignIndexSignatureToOptionalProperty.ts(17,5): error TS2322: Type 'Bar' is not assignable to type 'Foo'. | ||
Index signature is incompatible with property 'b'. | ||
Type 'number' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/compiler/assignIndexSignatureToOptionalProperty.ts (1 errors) ==== | ||
// #27144 | ||
|
||
interface Foo { | ||
a: number; | ||
b?: string; | ||
} | ||
interface Foo2 { | ||
a: number; | ||
b?: number; | ||
} | ||
interface Bar { | ||
a: number; | ||
[n: string]: number; | ||
} | ||
let b: Bar = { a: 42, b: 43 }; | ||
// Error, index signature does not match optional property `b` | ||
let f: Foo = b; | ||
~ | ||
!!! error TS2322: Type 'Bar' is not assignable to type 'Foo'. | ||
!!! error TS2322: Index signature is incompatible with property 'b'. | ||
!!! error TS2322: Type 'number' is not assignable to type 'string'. | ||
// OK | ||
let f2: Foo2 = b; | ||
|
29 changes: 29 additions & 0 deletions
29
tests/baselines/reference/assignIndexSignatureToOptionalProperty.js
This file contains 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,29 @@ | ||
//// [assignIndexSignatureToOptionalProperty.ts] | ||
// #27144 | ||
|
||
interface Foo { | ||
a: number; | ||
b?: string; | ||
} | ||
interface Foo2 { | ||
a: number; | ||
b?: number; | ||
} | ||
interface Bar { | ||
a: number; | ||
[n: string]: number; | ||
} | ||
let b: Bar = { a: 42, b: 43 }; | ||
// Error, index signature does not match optional property `b` | ||
let f: Foo = b; | ||
// OK | ||
let f2: Foo2 = b; | ||
|
||
|
||
//// [assignIndexSignatureToOptionalProperty.js] | ||
// #27144 | ||
var b = { a: 42, b: 43 }; | ||
// Error, index signature does not match optional property `b` | ||
var f = b; | ||
// OK | ||
var f2 = b; |
48 changes: 48 additions & 0 deletions
48
tests/baselines/reference/assignIndexSignatureToOptionalProperty.symbols
This file contains 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,48 @@ | ||
=== tests/cases/compiler/assignIndexSignatureToOptionalProperty.ts === | ||
// #27144 | ||
|
||
interface Foo { | ||
>Foo : Symbol(Foo, Decl(assignIndexSignatureToOptionalProperty.ts, 0, 0)) | ||
|
||
a: number; | ||
>a : Symbol(Foo.a, Decl(assignIndexSignatureToOptionalProperty.ts, 2, 15)) | ||
|
||
b?: string; | ||
>b : Symbol(Foo.b, Decl(assignIndexSignatureToOptionalProperty.ts, 3, 14)) | ||
} | ||
interface Foo2 { | ||
>Foo2 : Symbol(Foo2, Decl(assignIndexSignatureToOptionalProperty.ts, 5, 1)) | ||
|
||
a: number; | ||
>a : Symbol(Foo2.a, Decl(assignIndexSignatureToOptionalProperty.ts, 6, 16)) | ||
|
||
b?: number; | ||
>b : Symbol(Foo2.b, Decl(assignIndexSignatureToOptionalProperty.ts, 7, 14)) | ||
} | ||
interface Bar { | ||
>Bar : Symbol(Bar, Decl(assignIndexSignatureToOptionalProperty.ts, 9, 1)) | ||
|
||
a: number; | ||
>a : Symbol(Bar.a, Decl(assignIndexSignatureToOptionalProperty.ts, 10, 15)) | ||
|
||
[n: string]: number; | ||
>n : Symbol(n, Decl(assignIndexSignatureToOptionalProperty.ts, 12, 5)) | ||
} | ||
let b: Bar = { a: 42, b: 43 }; | ||
>b : Symbol(b, Decl(assignIndexSignatureToOptionalProperty.ts, 14, 3)) | ||
>Bar : Symbol(Bar, Decl(assignIndexSignatureToOptionalProperty.ts, 9, 1)) | ||
>a : Symbol(a, Decl(assignIndexSignatureToOptionalProperty.ts, 14, 14)) | ||
>b : Symbol(b, Decl(assignIndexSignatureToOptionalProperty.ts, 14, 21)) | ||
|
||
// Error, index signature does not match optional property `b` | ||
let f: Foo = b; | ||
>f : Symbol(f, Decl(assignIndexSignatureToOptionalProperty.ts, 16, 3)) | ||
>Foo : Symbol(Foo, Decl(assignIndexSignatureToOptionalProperty.ts, 0, 0)) | ||
>b : Symbol(b, Decl(assignIndexSignatureToOptionalProperty.ts, 14, 3)) | ||
|
||
// OK | ||
let f2: Foo2 = b; | ||
>f2 : Symbol(f2, Decl(assignIndexSignatureToOptionalProperty.ts, 18, 3)) | ||
>Foo2 : Symbol(Foo2, Decl(assignIndexSignatureToOptionalProperty.ts, 5, 1)) | ||
>b : Symbol(b, Decl(assignIndexSignatureToOptionalProperty.ts, 14, 3)) | ||
|
42 changes: 42 additions & 0 deletions
42
tests/baselines/reference/assignIndexSignatureToOptionalProperty.types
This file contains 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,42 @@ | ||
=== tests/cases/compiler/assignIndexSignatureToOptionalProperty.ts === | ||
// #27144 | ||
|
||
interface Foo { | ||
a: number; | ||
>a : number | ||
|
||
b?: string; | ||
>b : string | ||
} | ||
interface Foo2 { | ||
a: number; | ||
>a : number | ||
|
||
b?: number; | ||
>b : number | ||
} | ||
interface Bar { | ||
a: number; | ||
>a : number | ||
|
||
[n: string]: number; | ||
>n : string | ||
} | ||
let b: Bar = { a: 42, b: 43 }; | ||
>b : Bar | ||
>{ a: 42, b: 43 } : { a: number; b: number; } | ||
>a : number | ||
>42 : 42 | ||
>b : number | ||
>43 : 43 | ||
|
||
// Error, index signature does not match optional property `b` | ||
let f: Foo = b; | ||
>f : Foo | ||
>b : Bar | ||
|
||
// OK | ||
let f2: Foo2 = b; | ||
>f2 : Foo2 | ||
>b : Bar | ||
|
18 changes: 18 additions & 0 deletions
18
tests/baselines/reference/assignRestElementToOptionalProperty.errors.txt
This file contains 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,18 @@ | ||
tests/cases/compiler/assignRestElementToOptionalProperty.ts(5,5): error TS2322: Type '[number, ...string[]]' is not assignable to type '[number, number?, ...string[]]'. | ||
Rest element type is incompatible with property '1'. | ||
Type 'string' is not assignable to type 'number'. | ||
|
||
|
||
==== tests/cases/compiler/assignRestElementToOptionalProperty.ts (1 errors) ==== | ||
// Inspired by #27144 | ||
|
||
let t: [number, ...string[]]; | ||
// Error, rest type of `t` does not match element 1 of `t2` | ||
let t2: [number, number?, ...string[]] = t; | ||
~~ | ||
!!! error TS2322: Type '[number, ...string[]]' is not assignable to type '[number, number?, ...string[]]'. | ||
!!! error TS2322: Rest element type is incompatible with property '1'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'number'. | ||
// OK | ||
let t3: [number, string?, ...string[]] = t; | ||
|
17 changes: 17 additions & 0 deletions
17
tests/baselines/reference/assignRestElementToOptionalProperty.js
This file contains 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,17 @@ | ||
//// [assignRestElementToOptionalProperty.ts] | ||
// Inspired by #27144 | ||
|
||
let t: [number, ...string[]]; | ||
// Error, rest type of `t` does not match element 1 of `t2` | ||
let t2: [number, number?, ...string[]] = t; | ||
// OK | ||
let t3: [number, string?, ...string[]] = t; | ||
|
||
|
||
//// [assignRestElementToOptionalProperty.js] | ||
// Inspired by #27144 | ||
var t; | ||
// Error, rest type of `t` does not match element 1 of `t2` | ||
var t2 = t; | ||
// OK | ||
var t3 = t; |
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/assignRestElementToOptionalProperty.symbols
This file contains 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,16 @@ | ||
=== tests/cases/compiler/assignRestElementToOptionalProperty.ts === | ||
// Inspired by #27144 | ||
|
||
let t: [number, ...string[]]; | ||
>t : Symbol(t, Decl(assignRestElementToOptionalProperty.ts, 2, 3)) | ||
|
||
// Error, rest type of `t` does not match element 1 of `t2` | ||
let t2: [number, number?, ...string[]] = t; | ||
>t2 : Symbol(t2, Decl(assignRestElementToOptionalProperty.ts, 4, 3)) | ||
>t : Symbol(t, Decl(assignRestElementToOptionalProperty.ts, 2, 3)) | ||
|
||
// OK | ||
let t3: [number, string?, ...string[]] = t; | ||
>t3 : Symbol(t3, Decl(assignRestElementToOptionalProperty.ts, 6, 3)) | ||
>t : Symbol(t, Decl(assignRestElementToOptionalProperty.ts, 2, 3)) | ||
|
16 changes: 16 additions & 0 deletions
16
tests/baselines/reference/assignRestElementToOptionalProperty.types
This file contains 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,16 @@ | ||
=== tests/cases/compiler/assignRestElementToOptionalProperty.ts === | ||
// Inspired by #27144 | ||
|
||
let t: [number, ...string[]]; | ||
>t : [number, ...string[]] | ||
|
||
// Error, rest type of `t` does not match element 1 of `t2` | ||
let t2: [number, number?, ...string[]] = t; | ||
>t2 : [number, number?, ...string[]] | ||
>t : [number, ...string[]] | ||
|
||
// OK | ||
let t3: [number, string?, ...string[]] = t; | ||
>t3 : [number, string?, ...string[]] | ||
>t : [number, ...string[]] | ||
|
This file contains 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
19 changes: 19 additions & 0 deletions
19
tests/cases/compiler/assignIndexSignatureToOptionalProperty.ts
This file contains 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,19 @@ | ||
// #27144 | ||
|
||
interface Foo { | ||
a: number; | ||
b?: string; | ||
} | ||
interface Foo2 { | ||
a: number; | ||
b?: number; | ||
} | ||
interface Bar { | ||
a: number; | ||
[n: string]: number; | ||
} | ||
let b: Bar = { a: 42, b: 43 }; | ||
// Error, index signature does not match optional property `b` | ||
let f: Foo = b; | ||
// OK | ||
let f2: Foo2 = b; |
This file contains 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,7 @@ | ||
// Inspired by #27144 | ||
|
||
let t: [number, ...string[]]; | ||
// Error, rest type of `t` does not match element 1 of `t2` | ||
let t2: [number, number?, ...string[]] = t; | ||
// OK | ||
let t3: [number, string?, ...string[]] = t; |
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.
what?
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.
ts.sys.watchFile is a function with a final parameter of type WatchOptions, which has a property of type WatchFileKind, a numeric enum. CompilerHost's watchFile, the target signature, has a final parameter of type CompilerOptions, which does not have a matching property, but does have an index signature that includes number. But number isn't assignable to an enum.
This is technically sound, because people could call
watchFile
with an options bug containing any number at all (or a lot of other types), butts.sys.watchFile
can only handle WatchFileKind on certain of its properties.However, I don't know how likely this error is to happen in practise. We haven't had trouble as far as we know.