let course: string = "typescript"
course = "python"
Let’s say you wanted to run this code in a browser, you would have to convert this from TypeScript to JavaScript. The browser understands JavaScript so you have to make this code production ready so that your users can run it in their browsers.
To do that, you can use the tsc
compiler (coming from the typescript
package):
npx tsc mycode.ts
This will convert mycode.ts
to mycode.js
:
let course: string
= "typescript"
course = "python"
let course
= "typescript"
course = "python
Now, you can use mycode.js
in your browser. During the convertion from TypeScript to JavaScript, the compiler would throw an error if there is a type error. If none, the convertion works fine, and this gives you confidence in how type safe your code is.
Again, if you’re using frameworks, you most likely don’t have to run this code. When I write TypeScript in Astro and React, I never have to run tsc
myself. The framework will automatically run the compiler for you in the framework’s build process. We’re only doing this because we are working with vanilla JavaScript.