typescript:SyntaxError: Unexpected identifier

I’m new to typescript, what I’m trying to do is change the type of the var and apply operation . I dont know the name of this process

let str;

str = "33";

(str as number).toString()

Maybe this is what you want:


Although it has nothing to do with TS, it’s just regular JS

Edit: the error you have in the title of your post, that looks like you’re trying to run Typescript code in a JavaScript environment. That isn’t going to work, because TS type annotations are [purposefully] not valid JS. It doesn’t look like a TS error (which would be prefixed with exactly where in a file the error was triggered + an error code you can look up, eg my-file.ts(2, 23): error TS1128: Declaration or statement expected.

"33" is clearly a string, not a number. TS knows it’s not a number and you can’t force it to pretend it is. If you want a number, use a number, not a completely different type of thing.

If you are saying it’s a number, you actually have to make it a number (cast it to a number type): the TS compiler is not stupid, it knows that "33" is definitely not a number at any point in that program. What @jenovs says is necessary here. Typescript is just a type system: all those types just get thrown away when it compiles, it won’t change your code for you.

You’re also declaring an undefined variable (let str;), and the type of that is unknown. You then assign a string to it. This isn’t an issue, TS will allow this. But if you’re going to initialise an undefined variable, give it a type to narrow it (let str: string;). However, I don’t really see the point of you having it here when you could just do let str = "33"; and TS would infer immediately that it was a string, but anyway

I’m trying to do type assertion but that’s not happening

Right, but you can’t pretend it’s a number, because it isn’t, and Typescript knows that. To coerce a value from one type to another, you actually need to do that, you can’t just say “this string is a number”, because it’s not a number. If you assert it is a number, then at some point it needs to actually be a number: there is no point in your program where there is anything other than a string there.

1 Like

oh get it but then whats is the use of type assertion if I cannot change the types to perform operations

What do you mean? Typescript doesn’t change how you convert things from one type to another, it’s exactly the same. If I try to do "33" / 3 without Typescript (for example), it’ll still break, because it doesn’t make any sense, it’s just that it’ll happen when the program runs instead of when the program compiles. You add type assertions if the compiler can’t infer what the type is automatically (so if you knew it was a number but there was no way for the compiler to automatically figure it out you could assert it was a number to prevent errors)

I get it but , the main use of type assertion is changing the type and performing operations AFAIK
eg:- I have "33" I want to make it 11 so I will use type assertion and perform a operation then It 'll get back to the previous type
this is my understanding of type assertion but I think I’m wrong , too wrong

But it’s a string, if you say "33" as number, that doesn’t change how JavaScript works, you are just pretending it’s a number, and it isn’t a number, so your program would be wrong.

1 Like

yes then how can I use type assertions

Why do you want to use them here? It makes no sense in this context. They’re a way to prevent TS inferring what the type a value is, ie you know better than the compiler what the type of that value is. So if it’s unknown or any, and you know it’s something more specific, you can use an assertion to say what the more specific type is so that the program compiles properly. You can’t say that one thing that is clearly of a very specific type is actually a different type, it makes no sense – "33" is a string, you don’t need Typescript to be able to see that. If you want it to be a number, you need to actually turn it into a number, nothing is going to do that for you

1 Like