-
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
Do not widen computed properties with template literal types #54706
Open
Andarist
wants to merge
8
commits into
microsoft:main
Choose a base branch
from
Andarist:dont-widen-computed-prop-wiht-patterns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4609a44
Do not widen computed properties with template literal types
Andarist 275410d
Simplify the implementation by using a `Set`
Andarist 6e0b733
Just use `getSymbolLinks`
Andarist adf7db9
Merge remote-tracking branch 'origin/main' into dont-widen-computed-p…
Andarist c12e8a7
Merge remote-tracking branch 'origin/main' into dont-widen-computed-p…
Andarist 2284f68
Merge remote-tracking branch 'origin/main' into dont-widen-computed-p…
Andarist 47cb307
add a basic generic test case
Andarist 1a97927
Merge remote-tracking branch 'origin/main' into dont-widen-computed-p…
Andarist 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
115 changes: 115 additions & 0 deletions
115
tests/baselines/reference/computedPropertyNamesTemplateLiteralTypes.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,115 @@ | ||
computedPropertyNamesTemplateLiteralTypes.ts(94,7): error TS2322: Type '{ [x: string]: string | number[]; name: string; }' is not assignable to type 'IDocument_46309'. | ||
'string' and '`added_${string}`' index signatures are incompatible. | ||
Type 'string | number[]' is not assignable to type 'number[] | undefined'. | ||
Type 'string' is not assignable to type 'number[]'. | ||
|
||
|
||
==== computedPropertyNamesTemplateLiteralTypes.ts (1 errors) ==== | ||
declare const str1: string; | ||
declare const pattern1: `foo${string}`; | ||
declare const pattern2: `foobar${string}`; | ||
declare const samepattern1: `foo${string}`; | ||
|
||
const obj1 = { | ||
[pattern1]: true, | ||
}; | ||
|
||
const obj2 = { | ||
[pattern1]: true, | ||
[str1]: 100, | ||
}; | ||
|
||
const obj3 = { | ||
[str1]: 100, | ||
[pattern1]: true, | ||
}; | ||
|
||
const obj4 = { | ||
[pattern1]: true, | ||
[pattern2]: "hello", | ||
}; | ||
|
||
const obj5 = { | ||
[pattern2]: "hello", | ||
[pattern1]: true, | ||
}; | ||
|
||
const obj6 = { | ||
[pattern1]: true, | ||
[pattern2]: "hello", | ||
other: 100, | ||
}; | ||
|
||
const obj7 = { | ||
[pattern1]: true, | ||
[pattern2]: "hello", | ||
fooooooooo: 100, | ||
}; | ||
|
||
const obj8 = { | ||
[pattern1]: true, | ||
[pattern2]: "hello", | ||
foobarrrrr: 100, | ||
}; | ||
|
||
const obj9 = { | ||
[pattern1]: true, | ||
[samepattern1]: "hello", | ||
}; | ||
|
||
const obj10 = { | ||
[pattern1]: true, | ||
} as const; | ||
|
||
const obj11 = { | ||
[pattern1]: 100, | ||
...obj9, | ||
}; | ||
|
||
const obj12 = { | ||
...obj9, | ||
[pattern1]: 100, | ||
}; | ||
|
||
const obj13 = { | ||
[pattern1]: 100, | ||
...{ | ||
[pattern2]: "hello", | ||
}, | ||
}; | ||
|
||
const obj14 = { | ||
[pattern1]: 100, | ||
...{ | ||
[pattern1]: true, | ||
[pattern2]: "hello", | ||
foobarrrrr: [1, 2, 3], | ||
}, | ||
}; | ||
|
||
// repro from https://github.com/microsoft/TypeScript/issues/46309 | ||
|
||
interface IDocument_46309 { | ||
name: string; | ||
[added_: `added_${string}`]: number[] | undefined; | ||
} | ||
|
||
const tech1_46309 = { | ||
uuid: "70b26275-5096-4e4b-9d50-3c965c9e5073", | ||
}; | ||
|
||
const doc_46309: IDocument_46309 = { | ||
~~~~~~~~~ | ||
!!! error TS2322: Type '{ [x: string]: string | number[]; name: string; }' is not assignable to type 'IDocument_46309'. | ||
!!! error TS2322: 'string' and '`added_${string}`' index signatures are incompatible. | ||
!!! error TS2322: Type 'string | number[]' is not assignable to type 'number[] | undefined'. | ||
!!! error TS2322: Type 'string' is not assignable to type 'number[]'. | ||
name: "", | ||
[`added_${tech1_46309.uuid}`]: [19700101], | ||
}; | ||
|
||
const doc2_46309: IDocument_46309 = { | ||
name: "", | ||
[`added_${tech1_46309.uuid}` as const]: [19700101], | ||
}; | ||
|
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 probably warrants the "breaking change" tag, since previously these'd make a
string
index signature get added, but now you get a more specific index signature. Which is, I believe, the goal of the change, it's just... maybe breaky? It's unclear.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.
Also there's the whole union thing - but the union thing can be a problem for another day.