microsoft / microsoft/TypeScript
wrong behavior about type parameters of `as` clause in a mapped type
Open
Nobody has claimed this yet.
Domain: Mapped Types
Help Wanted
Possible Improvement
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
🔎 Search Terms
type parameter as mapped type
🕗 Version & Regression Information
5.7.0-dev.20240821
⏯ Playground Link
💻 Code
type IsString<T> = T[] extends string[] ? true : false
type OmitStringKey<T extends object> = {
[K in keyof T as IsString<T[K]> extends true ? never: K]: T[K]
}
function f<T>(t: T): { data: T } {
type A = IsString<T> // deferred
type B = A extends true ? true : false // deferred
type C = IsString<{data: T}['data']> // deferred
return {} as OmitStringKey<{ data: T }> // `data` is omitted
}
🙁 Actual behavior
OmitStringKey<{ data: T }> omit data property. That means IsString<T> extends true in the as clause is true, while T is not decided yet.
🙂 Expected behavior
type evaluation should be deferred
Additional information about the issue
No response
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 by running the linked TypeScript Playground repro and compare the deferred aliases with the mapped type's as clause. Trace the compiler's type-parameter evaluation for OmitStringKey<{ data: T }>; done means the data property is not omitted before T is resolved, with regression coverage added in the relevant TypeScript tests.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 32/100