-
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
[Proposal] Typescript Overriding Type #50989
Comments
You can already do |
|
I would do something like this interface User {
username: string | string[],
phone: string
}
type ShouldExtend<T, O> = T extends O ? T : never;
type Specify<T, O extends Record<string, any>> = ShouldExtend<Omit<T, keyof O> & O, T>;
function acceptUserWithStringName(u: Specify<User, {username: string}>): string {
return u.username;
}
const u1 = {
username: ['hello'],
phone: '123'
}
const u2 = {
username: 'miami',
phone: '456'
};
acceptUserWithStringName(u1);
acceptUserWithStringName(u2); |
Hi @fatcerberus π |
Intersecting with a new property doesn't behave the same way as // this type came from a library, I need to replace foo: any
type A = {
foo: any
}
type B = A & { foo: number }
type T = B['foo'] // I need this to be number, but it's still any
type Props = {
b: A
} And I want to constrain the type of type Props = {
b: Omit<A, 'foo'> & { foo: number },
} As such when there's more than one property to override, the |
what if it's be a reusable keyword? π€ // basic
interface One {
property: number;
}
// replace
interface Two extends One {
override property: string ;
}
// union mode
interface Three extends One {
extends property: string;
}
// cases
assertTypeEqual<One, { property: number }>;
assertTypeEqual<Two, { property: string }>;
assertTypeEqual<Three, { property: number | string }>; |
Typescript Overriding the Type
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Sometimes we need to
override
the some property in same interface with another type; ππ Motivating Example
playground
π» Use Cases
We can have this type alias as Type Tools :
And, We have an interface alike below :
Then, We need the same interface with some another type for
username
propertyBut If we use the
Override
type?Or This Syntax :
Or we have an
ICustomer
interface with same properties withIUser
but forusername
property that should be given thenumber
as the typeAlso We can use alike below syntax :
Work with Type Assignment :
Or any another example for this
Overidde
Utility Type, ThanksExactly this is just a Concept and with Week Example (take it easy π)
The text was updated successfully, but these errors were encountered: