-
-
Notifications
You must be signed in to change notification settings - Fork 546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Template literal type ideas #134
Comments
Some additional ideas from this other playground: type UndefinedToEmptyString<T extends string> = T extends undefined ? "" : T; // This one can probably be done better
type CamelCaseStringArray<K extends string[]> = `${K[0]}${Capitalize<UndefinedToEmptyString<K[1]>>}`;
type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K, "-"|"_"|" ">> : K;
type CamelCasedProps<T> = { [K in keyof T as CamelCase<K>]: T[K] }; Discussed in microsoft/TypeScript#40710 |
|
Even more ideas from this updated playground: type Join<S extends any[], D extends string> = string[] extends S ? string : S extends [`${infer T}`, ...infer U] ? U[0] extends undefined ? T : `${T}${D}${Join<U, D>}` : ''; As well as a somewhat convoluted |
These are super cool and useful. PRs welcome for at least camelcase, kebabcase, and trim. We can expose the helper utilities later on after more discussion. |
As dicussed in TypeScript 4.1 type ideas sindresorhus#134
As dicussed in TypeScript 4.1 type ideas sindresorhus#134
@voxpelli Do you happen to know whether TS 4.1 will make it possible to correctly get types for key paths? For example, |
Actually, I found an example of how to do it. Moving this to a new issue: #147 |
I commented over at microsoft/TypeScript#40710 (comment), but I figure I'll put this here as well. The latest iteration of the interface KebabCased {
"-webkit-animation": string;
"--main-bg-color": string;
"something--else": string;
}
// Becomes
// {
// Webkit: string;
// '': number;
// something: string;
// }
type CamelCased = CamelCasedProps<KebabCased>; I was working on my own type Separator = ' ' | '-' | '_';
type CamelCase<T extends string> =
T extends `${Separator}${infer Suffix}`
? CamelCase<Suffix>
: T extends `${infer Prefix}${Separator}`
? CamelCase<Prefix>
: T extends `${infer Prefix}${Separator}${infer Suffix}`
? CamelCase<`${Prefix}${Capitalize<Suffix>}`>
: T;
type CamelCasedProps<T> = { [K in keyof T as `${CamelCase<string & K>}`]: T[K] }
type SnakeObject = {
'-webkit-animation': string;
'--main-bg-color': string;
'something--else': string;
}
// Becomes
// {
// webkitAnimation: string;
// mainBgColor: number;
// somethingElse: string;
// }
type CamelObject = CamelCasedProps<SnakeObject>; See this playground. |
I ran into an issue with the current ( Other strings this fails for with even fatter unions are I think it's due to S extends I came up with my own implementation but it results in:
error for strings that are longer than 15 characters 😞 Not sure which is worse but I can make a PR if you guys would be interested in the other approach 😅 |
@kdmadej Would you be able to submit a pull request with one or more failing tests? That would help getting this fixed faster. |
IsoDate, IsoDateTime, IsoDateTimeUTC, etc. type Int = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0;
type Month = `0${Int}` | `1${0 | 1 | 2}`;
type Day = `${0 | 1 | 2}${Int}` | `3${0 | 1}`;
type IsoDate = `${Int}${Int}${Int}${Int}-${Month}-${Day}`; This can clearly get pretty hardcore. Maybe just limit it to |
Where do you get such great knowledge about TS types? |
@danielo515 The TS docs, but especially the release notes. That's where the cutting edge stuff happens, and a lot of it never actually makes it into the docs themselves. |
Not sure if this is possible with released types, but I would really love to see export type ExcludePrefix<T, U extends string> = T extends `${U}${infer _K}` ? never : T;
export type OmitPrefix<T, K extends string> = Pick<T, ExcludePrefix<keyof T, K>>; |
It seems that a lot of these have been implemented. If some haven't, it's best to open dedicated issues for each. |
TypeScript 4.1 will bring a lot of cool abilities: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/
Let's brainstorm some ideas.
For example, some ideas from this playground;
Upvote & Fund
The text was updated successfully, but these errors were encountered: