Reputation: 36836
How to transform an existing type into a new type with one property change?
Example:
type SomeComplexType = string // just for example
// cannot be changed
type Options<T> = {
opt1: boolean | undefined;
opt2: number;
opt3?: SomeComplexType;
opt4: T
}
// Can be changed
// How to change opt1 to accept only true and infer other option types?
type Keys = keyof Options<any>
let r: { [K in Keys]: K extends 'opt1' ? true : any }
// Good (should work)
r = { opt1: true, opt2: 2, opt3: '1', opt4: 1 }
r = { opt1: true, opt2: 2, opt3: '1', opt4: 'str' }
// Bad (should be error)
r = { opt1: false, opt2: 1, opt3: 'str', opt4: 1 } // opt1 should be true
r = { opt1: true, opt2: 'str', opt3: 'str', opt4: 1 } // opt2 should be number
r = { opt1: true, opt2: 'str', opt3: 1, opt4: 1 } // opt3 should be
Upvotes: 2
Views: 62
Reputation: 328152
If you have an object type O
and would like to make a new type where all the proptery keys and values are the same except that the property at key opt1
should be true
, you can write it like this:
{ [K in keyof O]: K extends 'opt1' ? true : O[K] }
The syntax O[K]
is an indexed access meaning "the type of the property of O
with a key of type K
".
Then your examples should work as desired, (assuming O
is Options<any>
):
// Good
r = { opt1: true, opt2: 2, opt3: '1', opt4: 1 } // okay
r = { opt1: true, opt2: 2, opt3: '1', opt4: 'str' } // okay
// Bad
r = { opt1: false, opt2: 1, opt3: 'str', opt4: 1 } // error!
r = { opt1: true, opt2: 'str', opt3: 'str', opt4: 1 } // error!
r = { opt1: true, opt2: 'str', opt3: 1, opt4: 1 } // error!
Upvotes: 1