文章目录
- 一、初识TypeScript
- 1、安装TypeScript
- 2、Hello TypeScript
- 二、结合项目---配置
- 1、tsconfig.json
- tsconfig.json 重要字段
- compilerOptions 每个选项的详细说明
- 2、ts-loader
- 三、语法
- 1、基本类型
- 2、类型注解
- 3、函数
- 4、接口
- 5、类
- 6、泛型
- 四、结合项目---vue3结合使用
一、初识TypeScript
1、安装TypeScript
首先,你需要在你的项目中安装TypeScript。你可以使用npm或者yarn来进行安装。在命令行中运行以下命令即可安装最新版本的TypeScript:
npm install -g typescript
2、Hello TypeScript
创建TypeScript文件index.ts
:
let text: string = 'Hello TypeScript'
tsc
为 typescript compiler
的缩写,即 TypeScript 编译器,用于将 TS 代码编译为 JS 代码。使用方法如下:
执行 tsc index.ts
命令,会在同目录下生成 index.js
文件。
tsc index.ts
index.js
var text = 'Hello TypeScript';
二、结合项目—配置
1、tsconfig.json
tsconfig.json
是TypeScript项目的配置文件,用于指定TypeScript编译器的选项和行为。
在TS的项目中,TS最终都会被编译JS文件执行,TS编译器在编译TS文件的时候都会先找项目根目录的tsconfig.json
文件,根据该文件的配置进行编译,默认情况下,如果该文件没有任何配置,TS编译器会默认编译项目目录下所有的.ts、.tsx、.d.ts
文件。
- 配置后,
tsconfig.json
所在目录下的对应文件
会在运行tsc
命令时编译成js文件并输出到outDir
配置的目录下
tsconfig.json 重要字段
- files - 设置要编译的文件的名称;
- include - 设置需要进行编译的文件,支持路径模式匹配;
- exclude - 设置无需进行编译的文件,支持路径模式匹配;
- compilerOptions - 设置与编译流程相关的选项。
compilerOptions 每个选项的详细说明
{"compilerOptions": {/* 基本选项 */"target": "es5", // 指定 ECMAScript 目标版本: 'ES3' (default), 'ES5', 'ES6'/'ES2015', 'ES2016', 'ES2017', or 'ESNEXT'"module": "commonjs", // 指定使用模块: 'commonjs', 'amd', 'system', 'umd' or 'es2015'"lib": [], // 指定要包含在编译中的库文件"allowJs": true, // 允许编译 javascript 文件"checkJs": true, // 报告 javascript 文件中的错误"jsx": "preserve", // 指定 jsx 代码的生成: 'preserve', 'react-native', or 'react'"declaration": true, // 生成相应的 '.d.ts' 文件"sourceMap": true, // 生成相应的 '.map' 文件"outFile": "./", // 将输出文件合并为一个文件"outDir": "./", // 指定输出目录"rootDir": "./", // 用来控制输出目录结构 --outDir."removeComments": true, // 删除编译后的所有的注释"noEmit": true, // 不生成输出文件"importHelpers": true, // 从 tslib 导入辅助工具函数"isolatedModules": true, // 将每个文件做为单独的模块 (与 'ts.transpileModule' 类似)./* 严格的类型检查选项 */"strict": true, // 启用所有严格类型检查选项"noImplicitAny": true, // 在表达式和声明上有隐含的 any类型时报错"strictNullChecks": true, // 启用严格的 null 检查"noImplicitThis": true, // 当 this 表达式值为 any 类型的时候,生成一个错误"alwaysStrict": true, // 以严格模式检查每个模块,并在每个文件里加入 'use strict'/* 额外的检查 */"noUnusedLocals": true, // 有未使用的变量时,抛出错误"noUnusedParameters": true, // 有未使用的参数时,抛出错误"noImplicitReturns": true, // 并不是所有函数里的代码都有返回值时,抛出错误"noFallthroughCasesInSwitch": true, // 报告 switch 语句的 fallthrough 错误。(即,不允许 switch 的 case 语句贯穿)/* 模块解析选项 */"moduleResolution": "node", // 选择模块解析策略: 'node' (Node.js) or 'classic' (TypeScript pre-1.6)"baseUrl": "./", // 用于解析非相对模块名称的基目录"paths": {}, // 模块名到基于 baseUrl 的路径映射的列表"rootDirs": [], // 根文件夹列表,其组合内容表示项目运行时的结构内容"typeRoots": [], // 包含类型声明的文件列表"types": [], // 需要包含的类型声明文件名列表"allowSyntheticDefaultImports": true, // 允许从没有设置默认导出的模块中默认导入。/* Source Map Options */"sourceRoot": "./", // 指定调试器应该找到 TypeScript 文件而不是源文件的位置"mapRoot": "./", // 指定调试器应该找到映射文件而不是生成文件的位置"inlineSourceMap": true, // 生成单个 soucemaps 文件,而不是将 sourcemaps 生成不同的文件"inlineSources": true, // 将代码与 sourcemaps 生成到一个文件中,要求同时设置了 --inlineSourceMap 或 --sourceMap 属性/* 其他选项 */"experimentalDecorators": true, // 启用装饰器"emitDecoratorMetadata": true // 为装饰器提供元数据的支持}
}
2、ts-loader
这是用于 webpack
的 TypeScript 加载器,将 TypeScript 编译成 JavaScript。
ts-loader
在内部是调用了 TypeScript 的官方编译器 -- tsc
。所以,ts-loader
和tsc
是共享tsconfig.json
。
npm install typescript ts-loader --save-dev
configureWebpack: {resolve: {extensions: [".ts", ".tsx", ".js", ".json"]},module: {rules: [{ test: /\.ts$/, loader: "ts-loader" },]}
}
三、语法
官方文档
1、基本类型
TypeScript支持和JavaScript一样的基本数据类型,如number、string、boolean、null、undefined等。此外,它还支持更复杂的类型,如数组、元组、枚举、对象等。
2、类型注解
TypeScript中可以使用类型注解来指定变量的类型。
let age: number = 30;
let name: string = "John";
let isStudent: boolean = true;
3、函数
TypeScript中的函数可以指定参数类型和返回值类型。
function add(x: number, y: number): number {return x + y;
}
4、接口
接口是用于定义对象的结构和类型的。
interface Person {name: string;age: number;
}let person: Person = {name: "Alice",age: 25
};
5、类
TypeScript支持类的概念,它允许你使用面向对象的编程方式。
class Animal {name: string;constructor(name: string) {this.name = name;}makeSound() {console.log("Animal makes a sound");}
}
6、泛型
TypeScript支持泛型,它可以让你在编写代码时动态指定类型。
function identity<T>(arg: T): T {return arg;
}let output = identity<string>("hello");
四、结合项目—vue3结合使用
最详细的 Vue3 + TypeScript 使用教程【值得收藏】