1

I am currently learning JavaScript. I used to work with statically typed languages, so, naturally, I decided to start with TypeScript instead of JS. TypeScript is nice and it solves a lot of problems JS has, but this "weak typing" really triggers me.

It doesn't feel right that I can get away with this:

let int: number = 42
let str: string = "69"

console.log(int + str)

Is there a way to prevent this type of conversion from happening even in the TypeScript? I want to get an error when add string to an integer.

11
  • You'll get an error as soon as you try to use the result in a place where (only) a number is expected. Otherwise, something like "the answer is "+42+"!" is perfectly valid and normal code, TypeScript won't complain about it. Commented Jun 18, 2022 at 12:32
  • @Bergi It's valid yes, but because it invokes JS' type-juggling it's also undesirable. Commented Jun 18, 2022 at 12:33
  • 1
    What you're asking for was requested back in 2016 and is still open: github.com/microsoft/TypeScript/issues/7989 Commented Jun 18, 2022 at 12:35
  • 1
    @Bergi, hopefully you saw the comment just below that. Even though it's standard, but mainly "because JavaScript does it", it may not be that desirable. Commented Jun 18, 2022 at 12:42
  • 1
    I’m guessing eslint has something Commented Jun 18, 2022 at 12:49

2 Answers 2

2

No, there's no rule in TypeScript that would prevent your doing that; the result of the expression int + str is string.

Now, if you tried to assign that to a variable of type number, that would fail:

let int: number = 42
let str: string = "69"

let result: number = int + str;
//  ^ error: Type 'string' is not assignable to type 'number'.(2322)

Note that this has little to do with loose typing; many strongly-typed languages such as Java and C# do exactly the same thing, implicitly convert the number to a string when using + in that way. Java example | C# example. Granted JavaScript takes this ridiculously far, but that's more about the early ethos in the language having been to prefer conversion over throwing errors. (Both probably stem from the same underlying goal, though, which was to make the language really accommodating — too much so, we now know. :-) )

Sign up to request clarification or add additional context in comments.

5 Comments

"Note that this has little to do with loose typing; many strongly-typed languages such as Java and C# do exactly the same thing, implicitly convert the number to a string when using + in that way" - yes, but C# et al. doesn't do all this.
Language aside, I think the "issue" is the "overloaded" + operator. If I remember correctly, there were some proposals for custom operator overloading in JS or TS? LE: github.com/tc39/proposal-operator-overloading this is the only reference I found so far.
@Dai - Indeed, JavaScript's type conversion is ... quite wild. :-)
@Dai C# does many of those things as well
To make it clearer: once you use the + operator, you have to understand how it works and that it does indeed try to do type coercion behind the scenes. Whether this default behavior makes sense or not, it's up to the reader (writer?).
1

Typescript as noted above doesn’t have this but Eslint does: https://eslint.org/docs/rules/no-implicit-coercion and https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/eslint-plugin/docs/rules/restrict-plus-operands.md

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.