DEV Community

bin
bin

Posted on

[typescript]how to build a ts project

first of all, we need to ensure npm and node.js are correctly installed.

mac@MacBook-Pro ~ % node -v
v24.4.1
mac@MacBook-Pro ~ % npm -v
11.4.2
Enter fullscreen mode Exit fullscreen mode

then we need cd into our project dir and create a package.json

mac@MacBook-Pro Work % mkdir ts-demo
mac@MacBook-Pro Work % cd ts-demo 

Enter fullscreen mode Exit fullscreen mode

install ts

mac@MacBook-Pro ts-demo % npm install --save-dev typescript

Enter fullscreen mode Exit fullscreen mode

initialize a config file


mac@MacBook-Pro ts-demo % npx tsc --init


Created a new tsconfig.json                                                     
                                                                             TS 
You can learn more at https://aka.ms/tsconfig
Enter fullscreen mode Exit fullscreen mode

create src/index.ts

const greet = (name: string): string => {
  return `Hello, ${name}!`;
};

console.log(greet("TypeScript on Mac"));
Enter fullscreen mode Exit fullscreen mode

compile it:

mac@MacBook-Pro ts-demo % npx tsc
mac@MacBook-Pro ts-demo % node src/index.js
Hello, TypeScript on Mac!
Enter fullscreen mode Exit fullscreen mode

ok. now the first ts project has been succefully created

Top comments (0)