question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

ExtraOptions:

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.

Route:

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.

Router:

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.

RouterLinkActive:

Member Current type Proposed type Reason
routerLinkActive string | string[] string | readonly string[] It should accept readonly arrays.

RouterOutlet:

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 and Navigation are types instead of interfaces?
  • What about creating a type to not duplicate the accepted type of routerLink and Router#functions?
  • Could RouterOutlet accept an optional generic argument to avoid explicit any?
  • About immutability, do you have any pretension to make non-mutable properties like queryParams, queryParamMap, params and paramMap readonly?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:41
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
Airbladercommented, Aug 24, 2020

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.

2reactions
dev054commented, Sep 3, 2021

More 1 vote 🙏

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found