-
-
Notifications
You must be signed in to change notification settings - Fork 926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#1686: Updated TypeScript defs. #1688
Conversation
* Added type checked m(Component, Attrs) def. * Added Comp as better type checked alternative to Component. * Refactored VnodeDOM into Vnode. * Simplified various types into ComponentTypes. * Renamed A, S => Attrs, State * Minor fixes. * Added missing bits. * Added comments. * Formatted. * Added tests.
Suggestions for fixing CI? |
It was a flaky async test. Restarting Travis fixed it. |
Reverted removing VnodeDOM. It was silly. |
@andraaspar thanks, I think this looks really good at first glance. Passes all my own tests but have yet to try it on my current projects. Sorry about the Component definition, I struggled with that back and forth myself and in fact will likely use your type as that's my preferred style. However using vnode.state is really common in the community and strictNullChecks is always the recommended (if not most used?) setting for TS so it seemed a bit more flexible. I really won't have time to go through all the changes for a day or two but a couple notes for now:
I think children must be an array so I think the type should be For Vnodes, I'm not entirely sure what a Vnode.children type should be. One of mithril's own tests uses Thanks for catching a variety of other things like the RouteResolver @isiahmeadows any other comments? |
for vnodes, from the docs:
I don't know if there's a way to define a type for |
@spacejack Thank you for the feedback. According to the docs, the |
@pygy I have fixed
There should be, but maybe in a separate task. |
@lhorie @spacejack One other question: |
I just looked at the (The engine tolerates receiving vnodes without state, to some extent (the only problem is for non-component vnodes with stateful hooks, which will not work if the initial vnode didn't have a For The signature is |
@andraaspar if you define interfaces for both |
The only mandatory field that should be optional to match the docs is I have just tested and Mithril always provides the
I'd suggest
I think it could work. My only objection is that we already have |
I don't think vnode is importable as a standalone module, so we could just have a function signature for |
@adamvlasak regarding But it isn't so much what This made me think about the |
So one last item that was bugging me led me down a bit of a rabbit hole. I didn't like exporting the globals, as most people would be using it as a module rather than as a script file. I looked into ways around this, and as @isiahmeadows had noted to me earlier, there is a better, more current way to write type definitions. Instead of writing: declare const m: Mithril.Static;
declare module 'mithril' {
const m: Mithril.Static;
export = m;
} we should be doing: declare const Mithril: Mithril.Static;
export as namespace m;
export = Mithril; In which case, module usage would be: import * as m from 'mithril'
import {Component, Vnode, ...} from 'mithril' and script usage would be: const c: m.Component<{},{}> = {view(){return m('p', 'abc')}} Modules would not be polluted by the global definition or global namespace and both would be more succinct overall. However... that would likely involve a lot of re-writing for everyone involved and I doubt anyone wants to be pressured into doing that right away. So my suggestion would be to remove type definitions from mithril so it can go ahead with the next release, and we can keep using types from our own repos. Then we can pool our efforts into creating types the right way for DefinitelyTyped and reduce the noise level on the mithril.js repo. As @andraaspar said earlier, using DefintelyTyped allows users to choose their own types. I was personally pretty excited to have type definitions included with the framework I love, but at this point I don't think it's the right decision. |
Here's an example of how I think the definitions should be written: https://github.com/spacejack/mithril.d.ts/blob/improved/index.d.ts Stream: I haven't dealt with all the sub-modules yet. That would take some more work. |
@spacejack I have looked into the problem and it is possible to replace the default type declarations that come with a module. You can do it using the {
"compilerOptions": {
"baseUrl": ".",
"paths": {
"mithril*": [
"custom-types/my-mithril.d.ts"
]
}
}
} This means that users may choose a different type declaration even if one is shipped with Mithril by default. What I gather from the code sample you created is that ideally, we should have one or multiple .d.ts files next to each module .js file in the Mithril repo. Would that be acceptable to project members @lhorie @isiahmeadows ? |
Ideally I think we want .d.ts files for all importable sub-modules, including hyperscript.d.ts, route.d.ts, request.d.ts, etc. I haven't tried this yet so I'm not 100% sure how to write those. Either way I don't think official types should be included in the next release because I don't think they'll be ready. @lhorie and @tivac are pretty ambivalent toward TS, and I know they're pretty busy with core library stuff. I'm not sure how @pygy feels about it after getting roped into a TS project with me. :) But my gut feeling now is that types should live in DT. |
Let's go with that then. |
May I ask where is current version of .d.ts files being developed? I checked main DefinitelyTyped repo and it contains outdated typedefs. Probably, 0.xx since they still contain |
@andraaspar BTW, @spacejack is the one who's been the most involved in the TS definitions for the past while. @chebum Those are massively outdated, and @spacejack only recently started looking to replace those. Historically, our definitions have lived in the repo itself, though, so until he starts rectifying that, assume the most current one is what lives in |
@chebum you can install types from here for now if you want: https://github.com/spacejack/mithril.d.ts |
@spacejack @isiahmeadows thank you for the input, guys. |
This was overly limiting. It prevented use like this: