Skip to content

Instantly share code, notes, and snippets.

@xantiagoma
Created January 17, 2024 06:30
Show Gist options
  • Save xantiagoma/aadac77f006e293f839cd118ff686834 to your computer and use it in GitHub Desktop.
Save xantiagoma/aadac77f006e293f839cd118ff686834 to your computer and use it in GitHub Desktop.
type IsId<T extends string = string> = T extends `${infer U}${infer V}`
? Includes<IdAlphabet, U> extends true
? V extends ""
? U
: IsId<V> extends never
? never
: `${U}${IsId<V>}`
: never
: never;
type X = IsId<"holamundo45555">;
type Id<T extends string> = IsId<Lowercase<T>> | `${IsId<Lowercase<T>>}_${IsId<Lowercase<T>>}`;
type Id2 = Id<string>;
function checkId<T extends string>(id: T): IsId<T> {
return id as IsId<T>;
}
const a = checkId("holamundo");
const b = checkId("holamundo123");
const c = checkId("holamundo1234hola");
const d = checkId("Holamundo1234hola1234");
const id1ok: IsId = "holamundo" as const;
const id2ok: IsId = "holamundo123";
const id3ok: IsId = "holamundo1234hola";
const id4ok: IsId = "holamundo1234hola1234";
const id5ok: IsId = "123";
const id6ok: IsId = "1234hola";
const id7ok: IsId = "1234hola1234";
const id8ok: IsId = "1h2h3h4h5hc6h7h8h9hahbhchdh";
const id9ok: IsId = "1h2h3h4h5hc6h7h8h9hahbhchdh5";
const id10ok: IsId = "h2h3h4h5hc6h7h8h9hahbhchdh5";
const id11ok: IsId = "h2h3h4h5hc6h7h8h9hahbhchdh";
const id1wrong: IsId = "Holamundo";
const id2wrong: IsId = "holAmundo123";
const id3wrong: IsId = "holamundo1234Hola";
const id4wrong: IsId = "holaMundo1234hola1234";
const id5wrong: IsId = "12A3";
const id6wrong: IsId = "1234hHla";
const id7wrong: IsId = "1234hOla1234";
const id8wrong: IsId = "1h2h3h4h5Fhc6h7h8h9hahbhchdh";
const id9wrong: IsId = "1h2h3h4h5Chc6h7h8h9hahbhchdh5";
const id10wrong: IsId = "h2h3hC4h5hc6h7h8h9hahbhchdh5";
const id11wrong: IsId = "h2h3h4h5hYc6h7h8h9hahbhchdh";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment