DEV Community

loading...

TypeType: generate complex typescript type with ease

mistlog profile image mistlog ・1 min read

TypeType is designed to generate complex typescript type with ease.

/* 
  export type MyPick<T, Keys extends keyof T> = { [K in Keys]: T[K] } 
*/
export type function MyPick = (T, Keys extends keyof T) => ^{
    for(K in Keys) {
        return {
            key: K,
            value: T[K]
        }
    }
}

/* type mapped2 = { [K in Keys as `get${K}`]: () => string }*/
type mapped2 = ^{
    for(K in Keys) {
        return {
            key: `get${K}`,
            value: type () => string
        }
    }
}

type function _isNumberString = (text) => ^{
    if(text extends "") {
        return true
    } else if(text extends `${infer digit}${infer rest}`) {
        return ^{
            if(digit extends Digit) {
                return _isNumberString<rest>
            } else {
                return false
            }
        }
    } else {
        return false
    }
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide