Skip to content
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

feat(common): more verbose tsc error message on wrong provider declaration #10109

Conversation

micalevisk
Copy link
Member

@micalevisk micalevisk commented Aug 13, 2022

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

having the follow custom provider:

{
  provide: '',
  useClass: class {},
  inject: [],
}

triggers the TSC error:

image

image

which isn't that friendly for beginners on TS

What is the new behavior?

image

image

image

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I know that this kind of usage looks hacky and isn't common but I believe it is less cryptic (or maybe is not) :p

Inspired by the video CUSTOM ERRORS in TypeScript? - Advanced TypeScript

@coveralls
Copy link

Pull Request Test Coverage Report for Build 4cc0497a-cc99-46e5-a464-7a7b852d20df

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 93.826%

Totals Coverage Status
Change from base Build 7429007a-3efb-492d-a2a1-84f706a41b5f: 0.0%
Covered Lines: 6094
Relevant Lines: 6495

💛 - Coveralls

@kamilmysliwiec
Copy link
Member

Interesting - I'm wondering if that's a common practice to use this trick. What do you think about this @jmcdo29?

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 16, 2022

It doesn't seem common, but it does seem useful. I like the approach because of the more verbose error message too, and as we're already using never it shouldn't be a breaking change to swap over to this custom string

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Aug 16, 2022

I wonder if it's clean from TS (language) perspective. String Literal Types were not designed to serve as better error messages. Technically if we approve this PR, the following code will be OK from the compiler's/type definition standpoint:

{
  provide: {},
  useClass: RandomClass,
  inject: 'The `inject` option is only for factory providers!',
}

I'm not saying that I dislike it as it clearly makes the error message verbose, just thinking out loud.

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 16, 2022

That shouldn't be possible, right? We're using the string literal as a type on ClassProvider and ValueProvider, it shouldn't bleed into the FactoryProvider type, right?

@kamilmysliwiec
Copy link
Member

Just updated the code snippet @jmcdo29, I was referring to class-based providers of course! Thanks for pointing that out

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 16, 2022

Ah, okay. So yeah, technically someone could do that. What would be the implications if they did? We actually ignore the inject option with ClassProvider and ValueProvider, right? So it's not like it should cause any runtime problems, and would really just mean someone isn't reading what Typescript is saying.

@kamilmysliwiec
Copy link
Member

No implications at all (runtime-wise), that's correct!

@delucca
Copy link
Contributor

delucca commented Aug 16, 2022

There is already a proposition to handle such behavior:

The PR idea is pretty good! But, personally, I would keep never and wait for that PR to be merged 😄 Just to avoid being "hacky" and stumbling upon any strange corner case/security issue

@micalevisk micalevisk closed this Aug 16, 2022
@micalevisk micalevisk deleted the feat/improve-error-msg-on-provider-misusage branch August 16, 2022 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants