microsoft / microsoft/TypeScript
Certain conditional types allow unsound assignments
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
"conditional type" generic assignment
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about conditional types
⏯ Playground Link
💻 Code
type Box<T> = { value: T }
type UnboxInner<T> = T extends Box<infer R> ? UnboxInner<R> : { unboxed: T }
type Unbox<T> = UnboxInner<T>['unboxed']
declare function unbox<T>(x: T): Unbox<T>
function example<T>(x: T) {
let a = unbox(x)
a = 'this should be an error'
}
🙁 Actual behavior
In example we can assign arbitrary values to a, even though it's typed as Unbox<T>.
🙂 Expected behavior
Attempting to assign invalid values to a (just about anything, since its type depends on a type parameter) should cause a type error.
Additional information about the issue
These issues may possibly be related:
- #52021
- #35533
cc @webstrand
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the behavior with related issues #52021 and #35533. Trace conditional-type generic assignment checking in the TypeScript compiler; done means the shown assignment is rejected and a regression test covers the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100