typescript how to rewrite property type of interface
up vote
1
down vote
favorite
type Old = Other & {rewrite:number};
type New = Old & {rewrite:string};
Exected behavior:
type of New
: Other & {rewrite:string}
Actual behavior:
type of New
: Other & {rewrite:string & number}
typescript
add a comment |
up vote
1
down vote
favorite
type Old = Other & {rewrite:number};
type New = Old & {rewrite:string};
Exected behavior:
type of New
: Other & {rewrite:string}
Actual behavior:
type of New
: Other & {rewrite:string & number}
typescript
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
type Old = Other & {rewrite:number};
type New = Old & {rewrite:string};
Exected behavior:
type of New
: Other & {rewrite:string}
Actual behavior:
type of New
: Other & {rewrite:string & number}
typescript
type Old = Other & {rewrite:number};
type New = Old & {rewrite:string};
Exected behavior:
type of New
: Other & {rewrite:string}
Actual behavior:
type of New
: Other & {rewrite:string & number}
typescript
typescript
asked Nov 22 at 14:02
s97712
206
206
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
This is the by design behavior of intersection types. If the property exists on both members of the intersection the resulting property type will be an intersection of the original types.
To replace a property, we can first exclude it from the original type using Pick
and Exclude
:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number};
type New = Omit<Old, 'rewrite'> & {rewrite:string};
let n: New;
n.rewrite // string
n.a // number
We can ecen create a generic type to do the replacement if this is a common scenario:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number, rewrite2:number};
type Replace<T, TKey extends keyof T, TKeyType> = Omit<T, TKey> & Record<TKey, TKeyType>
type New = Replace<Old, 'rewrite', string> // replace one
type New2 = Replace<Old, 'rewrite' | 'rewrite2', string> // replace more
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
This is the by design behavior of intersection types. If the property exists on both members of the intersection the resulting property type will be an intersection of the original types.
To replace a property, we can first exclude it from the original type using Pick
and Exclude
:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number};
type New = Omit<Old, 'rewrite'> & {rewrite:string};
let n: New;
n.rewrite // string
n.a // number
We can ecen create a generic type to do the replacement if this is a common scenario:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number, rewrite2:number};
type Replace<T, TKey extends keyof T, TKeyType> = Omit<T, TKey> & Record<TKey, TKeyType>
type New = Replace<Old, 'rewrite', string> // replace one
type New2 = Replace<Old, 'rewrite' | 'rewrite2', string> // replace more
add a comment |
up vote
3
down vote
This is the by design behavior of intersection types. If the property exists on both members of the intersection the resulting property type will be an intersection of the original types.
To replace a property, we can first exclude it from the original type using Pick
and Exclude
:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number};
type New = Omit<Old, 'rewrite'> & {rewrite:string};
let n: New;
n.rewrite // string
n.a // number
We can ecen create a generic type to do the replacement if this is a common scenario:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number, rewrite2:number};
type Replace<T, TKey extends keyof T, TKeyType> = Omit<T, TKey> & Record<TKey, TKeyType>
type New = Replace<Old, 'rewrite', string> // replace one
type New2 = Replace<Old, 'rewrite' | 'rewrite2', string> // replace more
add a comment |
up vote
3
down vote
up vote
3
down vote
This is the by design behavior of intersection types. If the property exists on both members of the intersection the resulting property type will be an intersection of the original types.
To replace a property, we can first exclude it from the original type using Pick
and Exclude
:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number};
type New = Omit<Old, 'rewrite'> & {rewrite:string};
let n: New;
n.rewrite // string
n.a // number
We can ecen create a generic type to do the replacement if this is a common scenario:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number, rewrite2:number};
type Replace<T, TKey extends keyof T, TKeyType> = Omit<T, TKey> & Record<TKey, TKeyType>
type New = Replace<Old, 'rewrite', string> // replace one
type New2 = Replace<Old, 'rewrite' | 'rewrite2', string> // replace more
This is the by design behavior of intersection types. If the property exists on both members of the intersection the resulting property type will be an intersection of the original types.
To replace a property, we can first exclude it from the original type using Pick
and Exclude
:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number};
type New = Omit<Old, 'rewrite'> & {rewrite:string};
let n: New;
n.rewrite // string
n.a // number
We can ecen create a generic type to do the replacement if this is a common scenario:
type Omit<T, TKey extends keyof T> = Pick<T, Exclude<keyof T, TKey>>
type Other = { a: number };
type Old = Other & {rewrite:number, rewrite2:number};
type Replace<T, TKey extends keyof T, TKeyType> = Omit<T, TKey> & Record<TKey, TKeyType>
type New = Replace<Old, 'rewrite', string> // replace one
type New2 = Replace<Old, 'rewrite' | 'rewrite2', string> // replace more
answered Nov 22 at 14:09
Titian Cernicova-Dragomir
54.4k33251
54.4k33251
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432670%2ftypescript-how-to-rewrite-property-type-of-interface%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown