-
Notifications
You must be signed in to change notification settings - Fork 27.6k
AngularJs TypeScript typings and --strictFunctionTypes #16617
Comments
This allows users to write callbacks using those aliases instead of the full type. Having convinient aliases is required for having AngularJS in TS work better with strictFunctionTypes flag on. see: angular/angular.js#16617 Also having them next to each other shows the symmetry between the require and controller param better.
…ype. This allows users to write callbacks using those aliases instead of the full type. Having convinient aliases is required for having AngularJS in TS work better with strictFunctionTypes flag on. see: angular/angular.js#16617 Also having them next to each other shows the symmetry between the require and controller param better.
If one is fine with having to pass one extra type argument to The main benefits are that they don't have to cast at all when requiring built-in directives (and still get the correct types) and only having to define the mapping (directive name --> controller type) once for their custom directive. By being more specific about the shape of your This is what I am talking about: poc I could find a way to auto-infer the controller types for arrays, but we might be able to cover the most common cases (e.g. arrays of length 2, 3, 4). (For larger arrays, you would get the broader The main downside (that I can think of) is having to provide the extra type argument to |
@gkalpak Keep in mind that the keys of the require: { myProp: 'ngModel', yourProp: '^^someOtherDirective' } So I don't think anything can be done here. link: (..., controller) => {
const myController = controller as MyControllerType;
} This (without the redundant type annotation) is indeed the right thing to do. |
Nooo 😢 You ruined everything 😭
We might still be able to use the approach for |
I'm submitting a ...
TypeScript introduced a new strictness flag with TS 2.7 - https://github.com/Microsoft/TypeScript/wiki/What%27s-new-in-TypeScript#strict-function-types
It makes typechecking for callback based APIs much stricter. For example:
only errors when --strictFunctionTypes is turned on.
AngularJs has many callback based APIs, which will be checked at more strictly with the flag. Some real bugs will be exposed, but also it will start erring on many correct patterns. For example:
This is now an error because TS doesn't know that require and link are related. As written the typings require one handle the full spectrum of options in the link function
IController | IController[] | {[key: string]: IController}
.TS types are quite expressive and @gkalpak and I tried to write up some prototype of typings that are aware of the link between require and link:
But even with full conditional types, it is impossible to extend this for user-defined directives.
So as plan B, I am going to contribute some nice type aliases to DefinitelyTyped
type LinkFnControllerType = IController | IController[] | {[key: string]: IController}
and propose the canonical solution to be writing the link function as:
One added benefit is that there is an explicit 'as' cast to remind the reader to check with the 'require' and controller statements.
The text was updated successfully, but these errors were encountered: