MithrilJS / mithril.d.ts

Types for mithril.js
MIT License
80 stars 11 forks source link

Use generic defaults (TS 2.3) #18

Closed spacejack closed 6 years ago

spacejack commented 7 years ago

With Typescript 2.3 it should be possible to modify the Component type to:

export interface Component<Attrs = {}, State extends Lifecycle<Attrs, State> = {}> extends Lifecycle<Attrs, State> {
    //...

Meaning that you only need to declare Attrs and State if you need to. So a stateless component could be:

const c: m.Component<{title: string}> = {
    view ({attrs: {title}}) {
        return m('h1', title)
    }
}

Without needing to declare an empty State type.

A simple component without Attrs or State types should work as well:

const c: m.Component = {view() {return m('p', 'Hello!')}}
spacejack commented 7 years ago

Added generic-defaults branch.

@andraaspar @isiahmeadows

dead-claudia commented 7 years ago

@spacejack SGTM, although IMHO if it's possible, State should default to this rather than {}.

spacejack commented 7 years ago

Sadly that's not allowed: A 'this' type is available only in a non-static member of a class or interface.

Would've made for some very nice component type inference.

dead-claudia commented 7 years ago

@spacejack

I thought that wasn't the case, but at the time, I couldn't immediately remember.

spacejack commented 7 years ago

Unfortunately the DT test doesn't seem to allow TS version 2.3 yet, so I'll have to hold off on submitting until they do.

spacejack commented 6 years ago

@isiahmeadows @andraaspar Going to try this again since DT accepts typescript version 2.3 or later.

Here's the updated branch diff.

dead-claudia commented 6 years ago

Go for it. I'm basically a walking checkmark for anything not insane, and have been trying to get the DT devs to set up a thing where I don't have to review every Mithril-related PR there. (I forget which issue it is, but you should be able to find it if you're wondering what I'm talking about. It has to do with owners and permissions.)

andraaspar commented 6 years ago

Looks good!