-
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
Omit loses type information when used with index signature #45367
Comments
Thanks for the quick response and the discussion links! That helps me understand the problem more deeply: type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>> and Though, Pick<NamedPlayer, "extraLives" | string | number> The compiler treats So we're left with a situation where I need to do two separate Pick<NamedPlayer, string | number> & Pick<NamedPlayer, "extraLives"> I can see why this is a hard problem! I'm not so sure that it needs to be considered WAD, though. Is it crazy to suggest that Note that the change to |
This is not a bug, this is working as intended. A change to omit has been proposed and rejected already. There are implementations out there (even in the issues linked) that work as you want by first excluding the Index signature. |
Sorry, I realize I'm bringing up something that's been brought up before, but I'm looking through the linked issues and struggling to find what you're describing. Where was a change to |
The problem is that a mapped type doesn't "know" what's it's doing from a higher level; all it sees is a collection of keys and some transformation to do to those keys. Since we (humans) made a type alias and named it |
That's true, but I'm not sure how that explains why type NamedPlayer = {
name: string;
extraLives: number;
[key: string | number]: unknown;
}
// Current behavior of Omit<NamedPlayer, 'name'>
type OmitResult1 = {
[key: string | number]: unknown;
}
// Suggested behavior of Omit<NamedPlayer, 'name'>
type OmitResult2 = {
extraLives: number;
[key: string | number]: unknown;
} As far as I can tell, an In terms of implementation: Since keyof NamedPlayer; // string | number
propertyof NamedPlayer; // 'name' | 'extraLives' | Indexer<string | number> |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
π Search Terms
Omit, index signature
π Version & Regression Information
This bug is present in all versions currently available in the Playground that support
Omit
, from 3.5.1 to 4.4.0-beta.β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
TypeScript raises the error
Object is of type 'unknown'
when adding+ 1
top.extraLives
because it sees that field as having typeunknown
.π Expected behavior
The type generated by
Omit<NamedPlayer, 'name'>
should have the same type information asNamedPlayer
for all fields other thanname
.In other words,
Omit<NamedPlayer, 'name'>
should yield the same type asOmit<NamedPlayer, 'name'> & Pick<NamedPlayer, 'extraLives'>
:The text was updated successfully, but these errors were encountered: