0

I have the following interface A and need to create type that extends A, but overrides definition of property type

interface A {
    type: 'user';
    name: string;
}

type B = ???

let q: B = {
    type: 'admin',
    name: 'John'
}

How to do this?

2 Answers 2

1

You can use a combination of Omit in an intersection with another type that adds the property back with a different type:

type B = Omit<A, 'type'> & {
  type: "admin"
}

Playground Link

You can also create a more general version of this replace type:

type Replace<T, TOverride> = Omit<T, keyof TOverride> & TOverride

type B =  Replace<A,  {
  type: "admin"
}>

Playground Link

Sign up to request clarification or add additional context in comments.

Comments

1
type B = Omit<A, 'type'> & {type: 'admin'};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.