-
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
Index signatures contribute properties to unions #25307
Merged
sandersn
merged 2 commits into
master
from
index-signatures-contribute-properties-to-union
Jul 6, 2018
Merged
Changes from 1 commit
Commits
Show all changes
2 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,9 @@ var bb; | |
|
||
var bbb = new mod.Baz(); | ||
>bbb : Symbol(bbb, Decl(use.js, 5, 3)) | ||
>mod.Baz : Symbol(Baz) | ||
>mod : Symbol(mod, Decl(use.js, 0, 3)) | ||
>Baz : Symbol(Baz) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
=== tests/cases/conformance/jsdoc/mod1.js === | ||
// error | ||
|
30 changes: 30 additions & 0 deletions
30
tests/baselines/reference/unionTypeWithIndexSignature.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/conformance/types/union/unionTypeWithIndexSignature.ts(11,3): error TS2339: Property 'bar' does not exist on type 'Missing'. | ||
Property 'bar' does not exist on type '{ [s: string]: string; }'. | ||
tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts(14,4): error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property. | ||
|
||
|
||
==== tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts (2 errors) ==== | ||
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string }; | ||
declare var u: Two; | ||
u.foo = 'bye' | ||
u.baz = 'hi' | ||
type Three = { foo: number } | { [s: string]: string } | { [s: string]: boolean }; | ||
declare var v: Three; | ||
v.foo = false | ||
type Missing = { foo: number, bar: true } | { [s: string]: string } | { foo: boolean } | ||
declare var m: Missing; | ||
m.foo = 'hi' | ||
m.bar | ||
~~~ | ||
!!! error TS2339: Property 'bar' does not exist on type 'Missing'. | ||
!!! error TS2339: Property 'bar' does not exist on type '{ [s: string]: string; }'. | ||
type RO = { foo: number } | { readonly [s: string]: string } | ||
declare var ro: RO; | ||
ro.foo = 'not allowed' | ||
~~~ | ||
!!! error TS2540: Cannot assign to 'foo' because it is a constant or a read-only property. | ||
type Num = { '0': string } | { [n: number]: number } | ||
declare var num: Num; | ||
num[0] = 1 | ||
num['0'] = 'ok' | ||
|
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,31 @@ | ||
//// [unionTypeWithIndexSignature.ts] | ||
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string }; | ||
declare var u: Two; | ||
u.foo = 'bye' | ||
u.baz = 'hi' | ||
type Three = { foo: number } | { [s: string]: string } | { [s: string]: boolean }; | ||
declare var v: Three; | ||
v.foo = false | ||
type Missing = { foo: number, bar: true } | { [s: string]: string } | { foo: boolean } | ||
declare var m: Missing; | ||
m.foo = 'hi' | ||
m.bar | ||
type RO = { foo: number } | { readonly [s: string]: string } | ||
declare var ro: RO; | ||
ro.foo = 'not allowed' | ||
type Num = { '0': string } | { [n: number]: number } | ||
declare var num: Num; | ||
num[0] = 1 | ||
num['0'] = 'ok' | ||
|
||
|
||
//// [unionTypeWithIndexSignature.js] | ||
"use strict"; | ||
u.foo = 'bye'; | ||
u.baz = 'hi'; | ||
v.foo = false; | ||
m.foo = 'hi'; | ||
m.bar; | ||
ro.foo = 'not allowed'; | ||
num[0] = 1; | ||
num['0'] = 'ok'; |
87 changes: 87 additions & 0 deletions
87
tests/baselines/reference/unionTypeWithIndexSignature.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,87 @@ | ||
=== tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts === | ||
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string }; | ||
>Two : Symbol(Two, Decl(unionTypeWithIndexSignature.ts, 0, 0)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 0, 12)) | ||
>bar : Symbol(bar, Decl(unionTypeWithIndexSignature.ts, 0, 19)) | ||
>baz : Symbol(baz, Decl(unionTypeWithIndexSignature.ts, 0, 32)) | ||
>s : Symbol(s, Decl(unionTypeWithIndexSignature.ts, 0, 50)) | ||
|
||
declare var u: Two; | ||
>u : Symbol(u, Decl(unionTypeWithIndexSignature.ts, 1, 11)) | ||
>Two : Symbol(Two, Decl(unionTypeWithIndexSignature.ts, 0, 0)) | ||
|
||
u.foo = 'bye' | ||
>u.foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 0, 12)) | ||
>u : Symbol(u, Decl(unionTypeWithIndexSignature.ts, 1, 11)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 0, 12)) | ||
|
||
u.baz = 'hi' | ||
>u.baz : Symbol(baz, Decl(unionTypeWithIndexSignature.ts, 0, 32)) | ||
>u : Symbol(u, Decl(unionTypeWithIndexSignature.ts, 1, 11)) | ||
>baz : Symbol(baz, Decl(unionTypeWithIndexSignature.ts, 0, 32)) | ||
|
||
type Three = { foo: number } | { [s: string]: string } | { [s: string]: boolean }; | ||
>Three : Symbol(Three, Decl(unionTypeWithIndexSignature.ts, 3, 12)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 4, 14)) | ||
>s : Symbol(s, Decl(unionTypeWithIndexSignature.ts, 4, 34)) | ||
>s : Symbol(s, Decl(unionTypeWithIndexSignature.ts, 4, 60)) | ||
|
||
declare var v: Three; | ||
>v : Symbol(v, Decl(unionTypeWithIndexSignature.ts, 5, 11)) | ||
>Three : Symbol(Three, Decl(unionTypeWithIndexSignature.ts, 3, 12)) | ||
|
||
v.foo = false | ||
>v.foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 4, 14)) | ||
>v : Symbol(v, Decl(unionTypeWithIndexSignature.ts, 5, 11)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 4, 14)) | ||
|
||
type Missing = { foo: number, bar: true } | { [s: string]: string } | { foo: boolean } | ||
>Missing : Symbol(Missing, Decl(unionTypeWithIndexSignature.ts, 6, 13)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 7, 16)) | ||
>bar : Symbol(bar, Decl(unionTypeWithIndexSignature.ts, 7, 29)) | ||
>s : Symbol(s, Decl(unionTypeWithIndexSignature.ts, 7, 47)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 7, 71)) | ||
|
||
declare var m: Missing; | ||
>m : Symbol(m, Decl(unionTypeWithIndexSignature.ts, 8, 11)) | ||
>Missing : Symbol(Missing, Decl(unionTypeWithIndexSignature.ts, 6, 13)) | ||
|
||
m.foo = 'hi' | ||
>m.foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 7, 16), Decl(unionTypeWithIndexSignature.ts, 7, 71)) | ||
>m : Symbol(m, Decl(unionTypeWithIndexSignature.ts, 8, 11)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 7, 16), Decl(unionTypeWithIndexSignature.ts, 7, 71)) | ||
|
||
m.bar | ||
>m : Symbol(m, Decl(unionTypeWithIndexSignature.ts, 8, 11)) | ||
|
||
type RO = { foo: number } | { readonly [s: string]: string } | ||
>RO : Symbol(RO, Decl(unionTypeWithIndexSignature.ts, 10, 5)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 11, 11)) | ||
>s : Symbol(s, Decl(unionTypeWithIndexSignature.ts, 11, 40)) | ||
|
||
declare var ro: RO; | ||
>ro : Symbol(ro, Decl(unionTypeWithIndexSignature.ts, 12, 11)) | ||
>RO : Symbol(RO, Decl(unionTypeWithIndexSignature.ts, 10, 5)) | ||
|
||
ro.foo = 'not allowed' | ||
>ro.foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 11, 11)) | ||
>ro : Symbol(ro, Decl(unionTypeWithIndexSignature.ts, 12, 11)) | ||
>foo : Symbol(foo, Decl(unionTypeWithIndexSignature.ts, 11, 11)) | ||
|
||
type Num = { '0': string } | { [n: number]: number } | ||
>Num : Symbol(Num, Decl(unionTypeWithIndexSignature.ts, 13, 22)) | ||
>'0' : Symbol('0', Decl(unionTypeWithIndexSignature.ts, 14, 12)) | ||
>n : Symbol(n, Decl(unionTypeWithIndexSignature.ts, 14, 32)) | ||
|
||
declare var num: Num; | ||
>num : Symbol(num, Decl(unionTypeWithIndexSignature.ts, 15, 11)) | ||
>Num : Symbol(Num, Decl(unionTypeWithIndexSignature.ts, 13, 22)) | ||
|
||
num[0] = 1 | ||
>num : Symbol(num, Decl(unionTypeWithIndexSignature.ts, 15, 11)) | ||
>0 : Symbol('0', Decl(unionTypeWithIndexSignature.ts, 14, 12)) | ||
|
||
num['0'] = 'ok' | ||
>num : Symbol(num, Decl(unionTypeWithIndexSignature.ts, 15, 11)) | ||
>'0' : Symbol('0', Decl(unionTypeWithIndexSignature.ts, 14, 12)) | ||
|
108 changes: 108 additions & 0 deletions
108
tests/baselines/reference/unionTypeWithIndexSignature.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,108 @@ | ||
=== tests/cases/conformance/types/union/unionTypeWithIndexSignature.ts === | ||
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string }; | ||
>Two : Two | ||
>foo : { bar: true; } | ||
>bar : true | ||
>true : true | ||
>baz : true | ||
>true : true | ||
>s : string | ||
|
||
declare var u: Two; | ||
>u : Two | ||
>Two : Two | ||
|
||
u.foo = 'bye' | ||
>u.foo = 'bye' : "bye" | ||
>u.foo : string | { bar: true; } | ||
>u : Two | ||
>foo : string | { bar: true; } | ||
>'bye' : "bye" | ||
|
||
u.baz = 'hi' | ||
>u.baz = 'hi' : "hi" | ||
>u.baz : string | true | ||
>u : Two | ||
>baz : string | true | ||
>'hi' : "hi" | ||
|
||
type Three = { foo: number } | { [s: string]: string } | { [s: string]: boolean }; | ||
>Three : Three | ||
>foo : number | ||
>s : string | ||
>s : string | ||
|
||
declare var v: Three; | ||
>v : Three | ||
>Three : Three | ||
|
||
v.foo = false | ||
>v.foo = false : false | ||
>v.foo : string | number | boolean | ||
>v : Three | ||
>foo : string | number | boolean | ||
>false : false | ||
|
||
type Missing = { foo: number, bar: true } | { [s: string]: string } | { foo: boolean } | ||
>Missing : Missing | ||
>foo : number | ||
>bar : true | ||
>true : true | ||
>s : string | ||
>foo : boolean | ||
|
||
declare var m: Missing; | ||
>m : Missing | ||
>Missing : Missing | ||
|
||
m.foo = 'hi' | ||
>m.foo = 'hi' : "hi" | ||
>m.foo : string | number | boolean | ||
>m : Missing | ||
>foo : string | number | boolean | ||
>'hi' : "hi" | ||
|
||
m.bar | ||
>m.bar : any | ||
>m : Missing | ||
>bar : any | ||
|
||
type RO = { foo: number } | { readonly [s: string]: string } | ||
>RO : RO | ||
>foo : number | ||
>s : string | ||
|
||
declare var ro: RO; | ||
>ro : RO | ||
>RO : RO | ||
|
||
ro.foo = 'not allowed' | ||
>ro.foo = 'not allowed' : "not allowed" | ||
>ro.foo : any | ||
>ro : RO | ||
>foo : any | ||
>'not allowed' : "not allowed" | ||
|
||
type Num = { '0': string } | { [n: number]: number } | ||
>Num : Num | ||
>'0' : string | ||
>n : number | ||
|
||
declare var num: Num; | ||
>num : Num | ||
>Num : Num | ||
|
||
num[0] = 1 | ||
>num[0] = 1 : 1 | ||
>num[0] : string | number | ||
>num : Num | ||
>0 : 0 | ||
>1 : 1 | ||
|
||
num['0'] = 'ok' | ||
>num['0'] = 'ok' : "ok" | ||
>num['0'] : string | number | ||
>num : Num | ||
>'0' : "0" | ||
>'ok' : "ok" | ||
|
19 changes: 19 additions & 0 deletions
19
tests/cases/conformance/types/union/unionTypeWithIndexSignature.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 @@ | ||
// @strict: true | ||
type Two = { foo: { bar: true }, baz: true } | { [s: string]: string }; | ||
declare var u: Two; | ||
u.foo = 'bye' | ||
u.baz = 'hi' | ||
type Three = { foo: number } | { [s: string]: string } | { [s: string]: boolean }; | ||
declare var v: Three; | ||
v.foo = false | ||
type Missing = { foo: number, bar: true } | { [s: string]: string } | { foo: boolean } | ||
declare var m: Missing; | ||
m.foo = 'hi' | ||
m.bar | ||
type RO = { foo: number } | { readonly [s: string]: string } | ||
declare var ro: RO; | ||
ro.foo = 'not allowed' | ||
type Num = { '0': string } | { [n: number]: number } | ||
declare var num: Num; | ||
num[0] = 1 | ||
num['0'] = 'ok' |
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.
this would include symbols too...
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.
Specifically (from our discussion) symbol-named properties should not check index signatures, numeric-named properties should use the number index signature (if present); then they and all other properties should check for a string index signature. Similar code should already exist in the code that gets the index type for a property that's not found in element access lookup.
Here's an example: