Learning Typescript, what should I expect?

Hey everyone,
I’m nearly done with the JavaScript course on FCC and was thinking of picking up Typescript next (there’s a lot of jobs in my area that want Angular). How much harder is it to transition from JS to TS? Also, I should mention I have a little familiarity with Java and Kotlin, so types are not a new concept to me.

Given your experience, the transition to TypeScript shouldn’t be too challenging.

In my experience, the biggest pain point is having to define type definitions for external APIs.

Thanks. I haven’t messed with APIs too much yet so that’s probably going to be a pain point regardless :sweat_smile:

Hi,

It shouldn’t be too painful. You can install the types you need from @types/package (if it exists, it often does) and just start using it.

For example:

C:\> mkdir test
C:\> cd test
C:\> npm init -y

Change your index.js file to, for example, app.ts. Create the file app.ts.

In your package.json, add the command “dev”:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "tsc app.ts && node app.js"
  },

The Typescript compiler will compile your .ts file to app.js, and node will then run it, according to this command.

Install typescript and any types you need as devDependencies:

C:\> npm i --save-dev typescript @types/bcrypt
C:\> npm i bcrypt

Write some code:

import { hashSync } from 'bcrypt'

type User = {
    name: string
    age: number
    password: string
}

const pwd = hashSync('123456', 10)

const user: User = {
    name: 'xiaoming',
    age: 18,
    password: pwd
}

console.log(user)

Normally your types would go into a separate file and you would import what you need, for example the User type above, but that’s the idea.

Run the program:

PS C:\Users\james\src\test> npm run dev

> test@1.0.0 dev
> tsc app.ts && node app.js

{
  name: 'xiaoming',
  age: 18,
  password: '$2b$10$g3cv4J.P2yk8jDs7oPAjCe7/1GK0tEM9gtcTITJJDsRkXlevqclgu'
}
PS C:\Users\james\src\test>

Once you have the hang of adding types, it becomes second nature.

If you use Visual Studio Code or another decent code editor, you have code completions and everything you need.

Angular just adds a lot of additional stuff that Angular provides. It’s a nice, full featured framework. I would recommend learning about it to get a good job for sure!

2 Likes

Also, don’t forget to ask ChatGPT for some demos and advice! It’s a lot of fun.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.