Reputation: 23813
Since template literals landed in Typescript (PR), we have access to the following functions in our types:
See documentation for more.
Knowing that, I suspect that what I want to achieve is not possible but asking in case I'm just missing something.
I've got an interface which have a set number of properties... Plus a few ones that could evolve. I need to able to extract in a type the ones that could evolve. The catch is that they come with incremental numbers (from 1 to 3). An example will be better to understand:
interface A {
propA: string; // not interested in that
propB: string; // not interested in that
propC: string; // not interested in that
propD1: string;
propD2: string;
propD3: string;
propE1: string;
propE2: string;
propE3: string;
propF1: string;
propF2: string;
propF3: string;
}
No idea how to implement it or if it's possible at all... But I'd like to have a type like this:
MagicType<A>
which should return 'propD' | 'propE' | 'propF'
.
Basically, it should be able to see that those exist with 1
, 2
, 3
at the end.
Pretty sure I'm asking for too much here but who knows :).
While that doesn't look doable with the existing template literal functions (Uppercase
, Lowercase
, Capitalize
, Uncapitalize
) I was also wondering if it's currently possible to create custom intrinsic types?
Thanks for any idea or even just confirming that's it's not doable at all
Upvotes: 6
Views: 1184
Reputation: 51083
"Intrinsic" means built into the compiler, so the strict answer is you would need to fork the compiler.
Of course the real answer is that you don't need your type to be intrinsic:
type EasyAs123<K extends string> = K extends `${infer U}${1 | 2 | 3}` ? U : never
// type Test = "propD" | "propE" | "propF"
type Test = EasyAs123<keyof A>
Upvotes: 10