创建 typescript 项目.md

有时候需要创建一个最简单的 typescript 项目来验证一些东西,这里记录一下如何创建一个最简单的 typescript 项目。

创建并初始化项目

$ mkdir my-typescript-project
$ cd my-typescript-project
$ npm init -y
$ npm install typescript ts-node @types/node --save-dev
$ npx tsc --init

创建 typescript 文件

  • hello.ts
const world = 'World';

export function hello(who: string = world): string {
    
    
  return `Hello ${
      
      who}! `;
}
  • index.ts
import {
    
     hello } from './hello';

console.log(hello('World'));

编译 typescript 文件

$ npx tsc

或者使用 -w 参数监控文件变化

$ npx tsc -w

运行 typescript 文件

$ npx ts-node index.ts

或者使用 nodemon 监控文件变化并编译运行

$ nodemon index.ts

猜你喜欢

转载自blog.csdn.net/kongxx/article/details/141970157