glutinum-org / cli

https://glutinum.net/
59 stars 6 forks source link

Support `create<T extends keyof TypeMap>` #66

Open MangelMaxime opened 8 months ago

MangelMaxime commented 8 months ago

TypeScript documentation:

interface TypeMap {
    "string": string;
    "number": number;
    "bool": Boolean;
}

export declare function create<T extends keyof TypeMap>(
    tagName: T,
): TypeMap[T];

// Usage
const t : string = create("string");
const t2 : string = create("number"); // Error because number is not assignable to string

translates into something like

[<Erase>]
type Exports =
    [<Import("create", "module")>]
    static member create (tagName: Exports.create.T) : obj = nativeOnly

module Exports =

    module create =

        [<RequireQualifiedAccess>]
        [<StringEnum(CaseRules.None)>]
        type T =
            | [<CompiledName("string")>] string
            | [<CompiledName("number")>] number
            | [<CompiledName("bool")>] bool

[<AllowNullLiteral>]
[<Interface>]
type TypeMap =
    abstract member string: string with get, set
    abstract member number: float with get, set
    abstract member bool: bool with get, set

Example of npm package using this features:

Leaflet