Skip to content
This repository has been archived by the owner on Jul 6, 2018. It is now read-only.

[WIP] Flesh out Component/Route/Controller typings #24

Merged
merged 20 commits into from
Sep 30, 2017

Conversation

theroncross
Copy link
Contributor

@theroncross theroncross commented Aug 28, 2017

  • Add Component class properties, methods, and events
  • Refactor CoreView to be a private class with correct extends
  • Move private Component 'uses' outside exported namespace
  • Add Component tests and comments from Ember API docs
  • Uses the same pattern to get Controller and Route exporting local and inherited public properties and methods
  1. General approach here was to use the extends and uses sections from the 2.14 api docs for structure:
    image
    became
    class Component extends CoreView.extend(ViewMixin, ActionSupport, ClassNamesSupport) {...}
    Note - api labeled private has not been included, but that includes TargetActionSupport#triggerAction which doesn't really seem private so much as 'not recommended'. 🤷‍♂️
  2. I have NOT checked them against 2.15.
  3. I added private classes and mixins outside the Ember namespace as interfaces with matching values (e.g. const ActionHandler: Ember.Mixin<ActionHandler>) where needed.
  4. Unfortunately, the scope of this PR grew a bit bigger than I'd hoped, including removing a couple of unused namespaces. Happy to move those changes to another PR if it's a problem.

@@ -1538,6 +1684,7 @@ namespace Ember {
*/
has(name: string): boolean;
}
const Route: Mixin<Route>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Route should still be a class, shouldn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - didn't mean to include that, though I think I'll need to make some changes.

const component1 = Component.extend({
actions: {
hello(name: string) {
console.log('Hello', name);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to test that the context gets set properly, e.g. by calling this.get('someComponentProperty')

**/
actions: ActionsHash;
}
const ActionHandler: ActionHandler;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be Mixin<ActionHandler>

}
const ActionSupport: ActionSupport;

interface ClassNamesSupport {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does ClassNamesSupport and ActionSupport come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok, I was testing on ember 2.9, those must have been introduced later.

@theroncross theroncross changed the title Flesh out Component typings [WIP] Flesh out Component/Route/Controller typings Aug 29, 2017
@theroncross
Copy link
Contributor Author

theroncross commented Aug 29, 2017

The route tests won't really match the docs until we have #9 and #20 merged. At this point I'm getting some nice help from VS Code when extending Component with an ES6 class, but Route and Controller are missing something that I'm not seeing.

As soon as #25 is merged I'll rebase this branch.

@theroncross
Copy link
Contributor Author

theroncross commented Aug 30, 2017

I need to grab the TemplateFactory type off of the htmlbars-inline-precompile module for the layout type in the Component class.

There's a PR in for grabbing types from function return values, but I'm not otherwise seeing a good way to do it without declaring a dummy type or just re-declaring the interface in the Ember module. Thoughts?

@dwickern
Copy link
Collaborator

You should be able to

import { TemplateFactory } from 'htmlbars-inline-precompile';

@theroncross theroncross changed the title [WIP] Flesh out Component/Route/Controller typings Flesh out Component/Route/Controller typings Sep 5, 2017
@theroncross
Copy link
Contributor Author

theroncross commented Sep 5, 2017

I'm getting failing tests from ember-inflector and qunit. Happy to fix them up, but they're way outside of my already over-sized scope here. Suggestions?

@theroncross theroncross changed the title Flesh out Component/Route/Controller typings [WIP] Flesh out Component/Route/Controller typings Sep 5, 2017
}
const ControllerMixin: Ember.Mixin<ControllerMixin>;

namespace Ember {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theroncross The tests pass locally for me if you change this line back to export namespace Ember and revert the ember-inflector tests changes in 5356bd0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'm still getting Cannot find name..s in the ember-qunit declarations for Hooks, Assert, and Qunit. I've diffed everything I can think of to see if I've accidentally changed something. Where are those declared?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What code are you referring to?

@theroncross
Copy link
Contributor Author

@dwickern I see that you added a static property to the Component class. Could you explain why?

@dwickern
Copy link
Collaborator

It's public API: Component.positionalParams

- [x] Add Component class properties, methods, and events
- [x] Refactor CoreView to be a private class with correct extends
- [x] Move private Component 'uses' outside exported namespace
- [x] (Unfortunately) had to move Controller pieces, too.
- [x] Add Component tests and comments from Ember API docs
The Controller and Route classes don't seem to be exporting all their
members in the way the Controller is, but I'm lacking the bandwidth
to spot the problem.
sendAction(action: string, context: any): void;
targetObject: Controller;
static positionalParams: string | string[];
class Component extends CoreView.extend(ViewMixin, ActionSupport, ClassNamesSupport) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should there be support for dynamic/runtime keys on the component? There will obviously be a loss of type-safety since they will be typed as {[key: string]: any} or is the mandate that key's be static with a default value? I feel like the former is more in line with how Ember works {{my-component dynamicPropKey=anyValue}}, but I think I might prefer the latter because of type-checking...

Copy link
Member

@chriskrycho chriskrycho Sep 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, if you add the looser type you lose all help from the compiler for get and so on, alas.

For TS to be useful people will need to declare what the component expects as its API in the class definition for it, because while you can set arbitrary keys on a component invocation, they’re essentially meaningless: nothing will ever be done with them.

For cases where people want to support adding arbitrary values at a given key, they can type that key as variousData: any in the component declaration and then it’ll be available within the component methods.

@theroncross
Copy link
Contributor Author

Transition isn't exported through any of the namespaced modules. Is that a problem?

@dwickern
Copy link
Collaborator

You're exporting it as Ember.Transition

LGTM

@theroncross
Copy link
Contributor Author

I was thinking specifically of the case in the component tests where I have to import the whole module to access Transition. Aren't these eventually going to be pulled out into smaller files?

@chriskrycho
Copy link
Member

SHIP IIIIIT!

@dwickern
Copy link
Collaborator

I suppose we could have a named export too. I don't want to do anything crazy, Transition is just a typescript interface, not a real API

@dwickern dwickern merged commit 11344dc into typed-ember:master Sep 30, 2017
@dwickern
Copy link
Collaborator

Thanks for solving the merge conflicts @theroncross !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants