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

Implicitly casting non-numbers with a PrefixUnaryExpression should fail typechecks #57347

Closed
cpojer opened this issue Feb 9, 2024 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@cpojer
Copy link

cpojer commented Feb 9, 2024

πŸ”Ž Search Terms

prefixunaryexpression

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAEAa0G8BQ1rAPYDsIBcBOArsDungBQAOBARiAJbDR4CmYAJliAJ5oF4uYcAYXS4AXNEwEAttWZ4AlIgC+SFNCq0G0AhTZgczMhnGSZcxYnWoWOPpknMA7nDI4AFnQgA6YHwHCojjQANRoQQoA3OqqqkgY2MEmwQC8iMkSAKzK0fFYuNAAHtBpmM6umVFqhd66+oZkALTJVWptbTV1BkbJ0ABU0I0AjFFAA

πŸ’» Code

class X {
  constructor(public readonly currentCost: number) {}

  public update(cost: number) {
    return new X(this.currentCost + cost);
  }
}

const cost = {cost: 5};

const x = new X(5);

x.update(-cost); // Should error

x.update(cost * -1); // Errors correctly

πŸ™ Actual behavior

I refactored some code from returning a number to returning an object. I expect TypeScript to point me to all the places where I needed to make adjustments and came across one where I had -cost where cost was changed from a number to an object. TypeScript was not giving me an error like it does when I use cost * -1.

πŸ™‚ Expected behavior

I would expect TypeScript to prevent implicit casts on non number types (or non-scalar types in case there are valid use cases of this for boolean/strings) if a PrefixUnaryExpression is used. There are some fun JavaScript things that can be done by using -[] or -{}, it is almost never what a developer intents.

Additional information about the issue

No response

@RyanCavanaugh
Copy link
Member

Duplicate #20131

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants