Better typings for router package
See original GitHub issue🚀 feature request
Relevant Package
@angular/router
Description
I’d want propose better typing for routes. I’ve summarized it all below:
Member | Current type | Proposed type | Reason |
---|---|---|---|
preloadingStrategy | any | Type<PreloadingStrategy> | A more concrete type than any . |
ActivatedRoute
/ActivatedRouteSnapshot
:
Member | Current type | Proposed type | Reason |
---|---|---|---|
component | Type<any> | string | null | Type<unknown> | null | component cannot be a string. |
Member | Current type | Proposed type | Reason |
---|---|---|---|
canActivate | any[] | readonly (string | Type<CanActivate>)[] | It should accept only string or (readonly, optionally) array of classes. |
canActivateChild | any[] | readonly (string | Type<CanActivateChild>)[] | It should accept only string or (readonly, optionally) array of classes. |
canDeactivate | any[] | readonly (string | Type<CanDeactivate>)[] | It should accept only string or (readonly, optionally) array of classes. |
canLoad | any[] | readonly (string | Type<CanLoad>)[] | It should accept only string or (readonly, optionally) array of classes. |
Member | Current type | Proposed type | Reason |
---|---|---|---|
createUrlTree | any[] | readonly (boolean | number | string | Record<‘outlets’, Record<string, readonly string[]>>)[] correct if I’m missing any accepted type here | it should accept only correct types. Also, it should accept readonly arrays. |
navigate | any[] | readonly (boolean | number | string | Record<‘outlets’, Record<string, readonly string[]>>)[] correct if I’m missing any accepted type here | It should accept only correct types. Also, it should accept readonly arrays. |
RouterLink
/RouterLinkWithHref
:
Member | Current type | Proposed type | Reason |
---|---|---|---|
routerLink | string | any[] | string | readonly (boolean | number | string | Record<‘outlets’, Record<string, readonly string[]>>)[] correct if I’m missing any accepted type here | It should accept only correct types. Also, it should accept readonly arrays. |
Member | Current type | Proposed type | Reason |
---|---|---|---|
routerLinkActive | string | string[] | string | readonly string[] | It should accept readonly arrays. |
Member | Current type | Proposed type | Reason |
---|---|---|---|
activate | EventEmitter<any> | EventEmitter<ComponentRef<unknown>> | A more concrete type than any . |
deactivate | EventEmitter<any> | EventEmitter< ComponentRef<unknown>> | A more concrete type than any . |
component | Object | ComponentRef<unknown> | A more concrete type than Object . |
provideRoutes
:
The return type should be ValueProvider
instead of any
.
Questions:
- Why
Data
andNavigation
are types instead of interfaces? - What about creating a type to not duplicate the accepted type of
routerLink
andRouter#functions
? - Could
RouterOutlet
accept an optional generic argument to avoid explicitany
? - About immutability, do you have any pretension to make non-mutable properties like
queryParams
,queryParamMap
,params
andparamMap
readonly
?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:41
- Comments:15 (6 by maintainers)
Top Results From Across the Web
@types/react-router-dom - npm
This package contains type definitions for react-router-dom (https://github.com/ReactTraining/react-router). Details. Files were exported from ...
Read more >Typescript custom @types package for @types/react-router
Now you can create a file to put your custom typings (e.g. custom-typings.d.ts) on your project and put that augmentation interface. Share.
Read more >How to Use React Router in Typescript | Pluralsight
In Typescript world, we have a better way.We can inspect the @types package and learn about the actual types of the parameters as...
Read more >Typescript Typings: The Complete Guide: @types Compiler ...
Does Typescript type safety necessarily mean more ceremony when writing code? ... How can packages provide their own custom types?
Read more >TypeScript - Fastify
We encourage users to send pull requests to improve typings support. ... Fastify offers two packages wrapping json-schema-to-ts and typebox :.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a quick thought: when doing this we should also consider strictNullChecks by adding undefined and null where possible / necessary. For compatibility that’ll be required I think.
More 1 vote 🙏