-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Rule proposal: restrict-constructor-expressions #10895
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
Comments
I can't search right now but this came up not long ago and I vaguely remember the team was broadly aligned that such a rule was niche and not broadly applicable enough to need its own rule.
|
I disagree that |
Our use case was this code: const postgresUid = Number(await execa`id -u postgres`); This was originally working, a few Fixed code was: const postgresUid = Number((await execa`id -u postgres`).stdout); It was surprising to me that no types or linting were preventing this |
I suppose you could do it with no-deprecated and some declaration merging to deprecate |
I'd personally just ban |
Yeah, I guess I will come up with my own home-grown solution to lint / type-check for Too bad that
I'm also of the opinion that |
I mean the type definition of It seems weird that you want a lint rule to ensure correct usage of |
There isn't a symmetry here though.
OTOH there is no It just doesn't seem broadly valuable to build a rule for this, IMO? |
@karlhorky FYI if you do end up doing syntactic bans of these functions I would suggest allowing the argument to be a |
I guess I disagree on that:
I think these types of "safety" rules (including also the rules for string interpolation) can be argued against with buried "best practices" comments on issues, but unless these best practices are exposed to users, users will keep getting bitten by this. My gut feel says most users won't know that it's possible to guard against this, so won't ask / search for this. Developing a rule would expose these best practices and offer high value right away to a wide range of codebases. |
A few points:
So now I think about it, I think we should have a no-base-to-string equivalent for number coercion that catches |
What is the resolution here? Just having a |
I'm a -1 because this is my 2c for codebases that care about it: {
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": ":matches(CallExpression, NewExpression) > Identifier[name = 'Number'].callee",
"message": "Don't use the number constructor, use parseInt/parseFloat"
},
{
"selector": "UnaryExpression[operator = '+']",
"message": "Don't use implicit number coercion, use parseInt/parseFloat"
}
]
}
} But I ofc won't block anyone progressing on this. |
Coming back to this, it's a really interesting discussion and we see the potential use. But Marking as |
Before You File a Proposal Please Confirm You Have Done The Following...
My proposal is suitable for this project
Description
Similar to
restrict-template-expressions
, the new rulerestrict-constructor-expressions
restrict the types of expressions passed to constructors such asNumber()
andString()
to a subset which result in non-surprising return valuesTypeScript types for constructors have a permissive
any
type for the parameters:Fail Cases
Pass Cases
Additional Info
No response
The text was updated successfully, but these errors were encountered: