dfilatov / vidom

Library to build UI based on virtual DOM
MIT License
415 stars 16 forks source link

defaultAttrs in typescript #352

Open LexRiver opened 5 years ago

LexRiver commented 5 years ago

What is the proper way to use defaultAttrs with typescript?

interface Attrs{
    value?:number
}

export class Test extends Component<Attrs, any, any>{
    static defaultAttrs:Partial<Attrs> = {
        value: 1
    }
}

so when I try to use this.attrs.value I'm getting Object is possibly 'undefined'

Seems like there is the solution for react, but not works for vidom https://mattferderer.com/default-props-in-react-with-typescript

dfilatov commented 5 years ago

Unfortunately there's no any good way to express defaultAttrs with TypeScript right now. You can use this.attrs.value! to make ts happy.

LexRiver commented 5 years ago

ok