-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
feat(common): more verbose tsc error message on wrong provider declaration #10109
Conversation
Pull Request Test Coverage Report for Build 4cc0497a-cc99-46e5-a464-7a7b852d20df
💛 - Coveralls |
Interesting - I'm wondering if that's a common practice to use this trick. What do you think about this @jmcdo29? |
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 |
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. |
That shouldn't be possible, right? We're using the string literal as a type on |
Just updated the code snippet @jmcdo29, I was referring to class-based providers of course! Thanks for pointing that out |
Ah, okay. So yeah, technically someone could do that. What would be the implications if they did? We actually ignore the |
No implications at all (runtime-wise), that's correct! |
There is already a proposition to handle such behavior:
The PR idea is pretty good! But, personally, I would keep |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
having the follow custom provider:
triggers the TSC error:
which isn't that friendly for beginners on TS
What is the new behavior?
Does this PR introduce a breaking change?
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