-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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' should alias a distinct mapped type (for display purposes) #31104
Comments
Actually it's the mapped type - you'd want to inline the type Omit<T, U> = {[K in Exclude<keyof T, U>]: T[K]}; ❤️ |
Yeah, I figured it out as I made the change. |
Looks like this is getting reverted because of #31190. |
Maaaaybe something like this? // default type parameter trickery
type Omit<T, K extends keyof any, X extends keyof T = Exclude<keyof T, K>> = {
[P in X]: T[X]
};
interface Test {
required: string;
optional?: string;
readonly viewonly: string;
}
declare const x: Omit<Test, "required" | "optional" | "notPresent" >;
x.viewonly = "okay"; // error!
// Cannot assign to 'viewonly' because it is a read-only property.
// typeof x is Omit<Test, "required" | "optional" | "notPresent", "viewonly">> Pro: The type parameter Con: Default type parameters are not meant for this, who knows what could happen Or what about // conditional type inference antics
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>> extends infer U
? { [P in keyof U]: U[P] }
: never;
interface Test {
required: string;
optional?: string;
readonly viewonly: string;
}
declare const x: Omit<Test, "required" | "optional" | "notPresent">;
x.viewonly = "okay"; // error!
// Cannot assign to 'viewonly' because it is a read-only property.
// typeof x is { readonly viewonly: string; } Pro: Still homomomomomorphic Con: Conditional type inference isn't exactly meant for this either, who knows what can happen |
The following type type Omit<T, ExcludedKeys extends keyof any> = {
[K in keyof T as Exclude<K, ExcludedKeys>]: T[K]
}; now maintains modifiers thanks to @ahejlsberg's pull request at #40633. |
I think this change will be contingent on performance tests though. |
Also, @weswigham may want to weigh in since he has a PR out at #37608, but I think it would be undesirable to add a 3rd type parameter just to use a default type argument. |
For TS 4.0 and older, you can use @Jack-Works’s type-challenges/type-challenges#141. |
Today,
Omit
will expand toPick<Exclude<...>, ...>
in quick info, error messages, etc. which is gross.By defining
Omit
as its own conditional type, we can get a slightly nicer display, though it will introduce duplication of code between the two helpers.The text was updated successfully, but these errors were encountered: