HerringtonDarkholme / av-ts

A modern, type-safe, idiomatic Vue binding library
MIT License
216 stars 11 forks source link

Try to improve `p` definition. #62

Closed HerringtonDarkholme closed 7 years ago

HerringtonDarkholme commented 7 years ago

Today I found a interesting solution:

type Class<P> = {__vueNew__(): P } | { new(...args: any[]): P & object }
type Prop<P> = Class<P> | {type: Class<P>, default?: P}
type Props<T> = {[K in keyof T]: Prop<T[K]>}

function getProps<T>(t: Props<T>): T {
    return null as any
}

interface StringConstructor {
    __vueNew__(): string
}

class Test {
    private testTag: string
}

var a = getProps({
    test: String,
    make: {
        type: String,
        default: 'ewewew'
    },
    wtf: Test
})

function needStr(a: string) {}
needStr(a.make)
a.make.charAt

The most interesting thing is the { new(...args: any[]): P & object }, the object typing makes TSC abstain from inferring String instead of string.

It could fix https://github.com/HerringtonDarkholme/av-ts/issues/59 https://github.com/HerringtonDarkholme/av-ts/issues/21

HerringtonDarkholme commented 7 years ago

Fixed in https://github.com/HerringtonDarkholme/av-ts/commit/7b4a40322de404ca9f62aa69f18a9014c1079b10