-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Relax requirements on index signatures to 'any' when a type also contains a string index signature to 'any' #43065
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
DanielRosenwasser
merged 4 commits into
master
from
alwaysBeHappyWithStringIndexSignatures
Mar 5, 2021
Merged
Changes from all commits
Commits
Show all changes
4 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
145 changes: 145 additions & 0 deletions
145
tests/baselines/reference/objectTypeWithStringAndNumberIndexSignatureToAny.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,145 @@ | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(43,5): error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
Index signature is missing in type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(49,5): error TS2739: Type 'StringTo<any>' is missing the following properties from type 'Obj': hello, world | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(50,5): error TS2739: Type 'NumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(51,5): error TS2739: Type 'StringAndNumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(61,5): error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(65,5): error TS2322: Type 'Obj' is not assignable to type 'StringTo<any> & NumberTo<any>'. | ||
Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
Index signature is missing in type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(67,5): error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(68,5): error TS2322: Type 'NumberTo<any>' is not assignable to type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(69,5): error TS2739: Type 'StringTo<any> & NumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(84,5): error TS2322: Type 'Obj' is not assignable to type 'NumberToNumber'. | ||
Index signature is missing in type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(88,5): error TS2322: Type 'Obj' is not assignable to type 'StringToAnyNumberToNumber'. | ||
Index signature is missing in type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(90,5): error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'. | ||
tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts(91,5): error TS2739: Type 'NumberTo<number>' is missing the following properties from type 'Obj': hello, world | ||
|
||
|
||
==== tests/cases/conformance/types/members/objectTypeWithStringAndNumberIndexSignatureToAny.ts (13 errors) ==== | ||
// When checking compatibility between two types, | ||
// TypeScript should not require an index signature if | ||
// the target side index signature maps to `any` *and* | ||
// the target side has *any* string index signature to `any`. | ||
// | ||
// So an index signature like in | ||
// | ||
// { [x: number]: any } | ||
// | ||
// is still required of a source type, but neither index signature in | ||
// | ||
// { [x: number]: any, [x: string]: any; } | ||
// | ||
// should be required; *however*, the number index signature in | ||
// | ||
// { [x: number]: number, [x: string]: any; } | ||
// | ||
// should always be required. | ||
|
||
interface StringTo<T> { | ||
[x: string]: T; | ||
} | ||
|
||
interface NumberTo<T> { | ||
[x: number]: T; | ||
} | ||
|
||
interface StringAndNumberTo<T> extends StringTo<T>, NumberTo<T> { | ||
} | ||
|
||
interface Obj { | ||
hello: string; | ||
world: number; | ||
} | ||
|
||
function f1(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringAndNumberTo<any>, someObj: Obj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
|
||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
~~~~~~ | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
!!! error TS2322: Index signature is missing in type 'Obj'. | ||
|
||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
|
||
someObj = sToAny; | ||
~~~~~~~ | ||
!!! error TS2739: Type 'StringTo<any>' is missing the following properties from type 'Obj': hello, world | ||
someObj = nToAny; | ||
~~~~~~~ | ||
!!! error TS2739: Type 'NumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
someObj = bothToAny; | ||
~~~~~~~ | ||
!!! error TS2739: Type 'StringAndNumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
} | ||
|
||
function f2(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringTo<any> & NumberTo<any>, someObj: Obj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
|
||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
~~~~~~ | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
|
||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
~~~~~~~~~ | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'StringTo<any> & NumberTo<any>'. | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberTo<any>'. | ||
!!! error TS2322: Index signature is missing in type 'Obj'. | ||
Comment on lines
+97
to
+101
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. This might be questionable. This used to work in 4.2, but this PR doesn't un-break this code. |
||
|
||
someObj = sToAny; | ||
~~~~~~~ | ||
!!! error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'. | ||
someObj = nToAny; | ||
~~~~~~~ | ||
!!! error TS2322: Type 'NumberTo<any>' is not assignable to type 'Obj'. | ||
someObj = bothToAny; | ||
~~~~~~~ | ||
!!! error TS2739: Type 'StringTo<any> & NumberTo<any>' is missing the following properties from type 'Obj': hello, world | ||
} | ||
|
||
type NumberToNumber = NumberTo<number>; | ||
|
||
interface StringToAnyNumberToNumber extends StringTo<any>, NumberToNumber { | ||
} | ||
|
||
function f3(sToAny: StringTo<any>, nToNumber: NumberToNumber, strToAnyNumToNum: StringToAnyNumberToNumber, someObj: Obj) { | ||
sToAny = nToNumber; | ||
sToAny = strToAnyNumToNum; | ||
sToAny = someObj; | ||
|
||
nToNumber = sToAny; | ||
nToNumber = strToAnyNumToNum; | ||
nToNumber = someObj; | ||
~~~~~~~~~ | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'NumberToNumber'. | ||
!!! error TS2322: Index signature is missing in type 'Obj'. | ||
|
||
strToAnyNumToNum = sToAny; | ||
strToAnyNumToNum = nToNumber; | ||
strToAnyNumToNum = someObj; | ||
~~~~~~~~~~~~~~~~ | ||
!!! error TS2322: Type 'Obj' is not assignable to type 'StringToAnyNumberToNumber'. | ||
!!! error TS2322: Index signature is missing in type 'Obj'. | ||
|
||
someObj = sToAny; | ||
~~~~~~~ | ||
!!! error TS2322: Type 'StringTo<any>' is not assignable to type 'Obj'. | ||
someObj = nToNumber; | ||
~~~~~~~ | ||
!!! error TS2739: Type 'NumberTo<number>' is missing the following properties from type 'Obj': hello, world | ||
someObj = someObj; | ||
} |
157 changes: 157 additions & 0 deletions
157
tests/baselines/reference/objectTypeWithStringAndNumberIndexSignatureToAny.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,157 @@ | ||
//// [objectTypeWithStringAndNumberIndexSignatureToAny.ts] | ||
// When checking compatibility between two types, | ||
// TypeScript should not require an index signature if | ||
// the target side index signature maps to `any` *and* | ||
// the target side has *any* string index signature to `any`. | ||
// | ||
// So an index signature like in | ||
// | ||
// { [x: number]: any } | ||
// | ||
// is still required of a source type, but neither index signature in | ||
// | ||
// { [x: number]: any, [x: string]: any; } | ||
// | ||
// should be required; *however*, the number index signature in | ||
// | ||
// { [x: number]: number, [x: string]: any; } | ||
// | ||
// should always be required. | ||
|
||
interface StringTo<T> { | ||
[x: string]: T; | ||
} | ||
|
||
interface NumberTo<T> { | ||
[x: number]: T; | ||
} | ||
|
||
interface StringAndNumberTo<T> extends StringTo<T>, NumberTo<T> { | ||
} | ||
|
||
interface Obj { | ||
hello: string; | ||
world: number; | ||
} | ||
|
||
function f1(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringAndNumberTo<any>, someObj: Obj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
|
||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
|
||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
|
||
someObj = sToAny; | ||
someObj = nToAny; | ||
someObj = bothToAny; | ||
} | ||
|
||
function f2(sToAny: StringTo<any>, nToAny: NumberTo<any>, bothToAny: StringTo<any> & NumberTo<any>, someObj: Obj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
|
||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
|
||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
|
||
someObj = sToAny; | ||
someObj = nToAny; | ||
someObj = bothToAny; | ||
} | ||
|
||
type NumberToNumber = NumberTo<number>; | ||
|
||
interface StringToAnyNumberToNumber extends StringTo<any>, NumberToNumber { | ||
} | ||
|
||
function f3(sToAny: StringTo<any>, nToNumber: NumberToNumber, strToAnyNumToNum: StringToAnyNumberToNumber, someObj: Obj) { | ||
sToAny = nToNumber; | ||
sToAny = strToAnyNumToNum; | ||
sToAny = someObj; | ||
|
||
nToNumber = sToAny; | ||
nToNumber = strToAnyNumToNum; | ||
nToNumber = someObj; | ||
|
||
strToAnyNumToNum = sToAny; | ||
strToAnyNumToNum = nToNumber; | ||
strToAnyNumToNum = someObj; | ||
|
||
someObj = sToAny; | ||
someObj = nToNumber; | ||
someObj = someObj; | ||
} | ||
|
||
//// [objectTypeWithStringAndNumberIndexSignatureToAny.js] | ||
"use strict"; | ||
// When checking compatibility between two types, | ||
// TypeScript should not require an index signature if | ||
// the target side index signature maps to `any` *and* | ||
// the target side has *any* string index signature to `any`. | ||
// | ||
// So an index signature like in | ||
// | ||
// { [x: number]: any } | ||
// | ||
// is still required of a source type, but neither index signature in | ||
// | ||
// { [x: number]: any, [x: string]: any; } | ||
// | ||
// should be required; *however*, the number index signature in | ||
// | ||
// { [x: number]: number, [x: string]: any; } | ||
// | ||
// should always be required. | ||
function f1(sToAny, nToAny, bothToAny, someObj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
someObj = sToAny; | ||
someObj = nToAny; | ||
someObj = bothToAny; | ||
} | ||
function f2(sToAny, nToAny, bothToAny, someObj) { | ||
sToAny = nToAny; | ||
sToAny = bothToAny; | ||
sToAny = someObj; | ||
nToAny = sToAny; | ||
nToAny = bothToAny; | ||
nToAny = someObj; | ||
bothToAny = sToAny; | ||
bothToAny = nToAny; | ||
bothToAny = someObj; | ||
someObj = sToAny; | ||
someObj = nToAny; | ||
someObj = bothToAny; | ||
} | ||
function f3(sToAny, nToNumber, strToAnyNumToNum, someObj) { | ||
sToAny = nToNumber; | ||
sToAny = strToAnyNumToNum; | ||
sToAny = someObj; | ||
nToNumber = sToAny; | ||
nToNumber = strToAnyNumToNum; | ||
nToNumber = someObj; | ||
strToAnyNumToNum = sToAny; | ||
strToAnyNumToNum = nToNumber; | ||
strToAnyNumToNum = someObj; | ||
someObj = sToAny; | ||
someObj = nToNumber; | ||
someObj = someObj; | ||
} |
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.
This is the example that's been un-broken.