Consider the two following type definitions (which I believe are equivalent):
type ChangePropertyType1<T, TKeys extends keyof T, TNew> =
Omit<T, TKeys> & { [K in TKeys]: TNew }
type ChangePropertyType2<T, TKeys extends keyof T, TNew> =
{ [K in keyof T]: K extends TKeys ? TNew: T[K] }
Both work, but the second one gives more useful type information in my IDE—it seems to go one level deeper.
But, the following type declaration seems to require the Omit style:
type RequireProperties<T, TKeys extends keyof T> =
Omit<T, TKeys> & Required<Pick<T, TKeys>>
Is there some way to rewrite this so that IDE hinting doesn't give less useful type information?
Here's an example of what "less useful type information" means when type information is displayed in IntelliJ/WebStorm. Consider these types:
interface Person {
name: string,
age: number
}
type Person1 = ChangePropertyType1<Person, 'age', string>
type Person2 = ChangePropertyType2<Person, 'age', string>
let person1: Person1
let person2: Person2
Hovering over Person1
gives:
Alias for: ChangePropertyType1<Person, "age", string>
Initial type: Omit<Person, "age"> & {age: string}
But hovering over Person2
gives the much more helpful:
Alias for: ChangePropertyType2<Person, "age", string>
Initial type: {name: Person["name"], age: string}
Similar results for person1
vs. person2
(let person1: Person1
, let person2: ChangePropertyType2<Person, "age", string>
, which is still not great, but does give one level deeper of type information)
Ideally, the type hint would (also?) show the final "computed type":
{
name: string
age: string
}