Issue
Suppose we have an interface A defined like this:
interface A = {
firstName: string;
lastName: string;
createdAt: Date;
}
How can I create a generic type that for any given interface, transforms all string properties to number?
Output
interface B = {
firstName: number;
lastName: number;
cretedAt: Date;
}
Solution
By using a type for B instead, you can map over the keys of A and conditionally use number if the value at that key is originally string.
interface A {
firstName: string;
lastName: string;
createdAt: Date;
}
type B = {
[K in keyof A]: A[K] extends string ? number : A[K]
}
Answered By - CertainPerformance
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.