Elixir and a few other languages have the pattern matching and type system that could pull that off but not a lot of languages do. Can you do that in typescript? I don’t see how.
export type inferSelectorElementName<sel extends selector> =
| string extends sel ? HTMLElement
: sel extends `${infer A},${infer B}` ? inferSelectorElementName<A | B>
: sel extends `${infer A}${'.' | '[' | '#'}${infer _}` ? inferSelectorElementName<A>
: sel
export type inferElementFromSelector<sel extends selector> =
| string extends sel ? HTMLElement
: inferSelectorElementName<sel> extends infer S ?
S extends '' ? HTMLElement
: S extends keyof HTMLElementTagNameMap ? HTMLElementTagNameMap[S]
: never
: never
TS types may go quite deep
Check Arktype library [https://arktype.io/], it's type definitions are basically a Typescript written in JSON const user = type({
name: "string",
platform: "'android' | 'ios'",
"version?": "number | string"
})- A SQL database implemented purely in TypeScript type system (https://github.com/codemix/ts-sql)
- Chess implemented entirely in TypeScript (and Rust) type systems (https://github.com/Dragon-Hatcher/type-system-chess)
- Lambda calculus in TypeScript type system (https://ayazhafiz.com/articles/21/typescript-type-system-lam...)