参考文档ts文档
安装
npm -g intsall typescript
第一个demo-基本语法特性
touch hello.ts
function sayHello(name: string, word?: string) {console.log(`${name} says ${word || 'hello'}`);}sayHello('Alan');sayHello('Tom', 'I love ts');
编译
tsc hello.ts
运行
node hello.js
// Alan says hello
// Tom says I love ts
什么是typescript(ts)?
ts是js的超集,它扩展了js语法,引入了强类型及编译校验。语法特性包括Class,interface, Type annotations, Compile time type checking, Arrow function等。
类型批注(type annotations)
ts通过类型批注提供静态类型编译时检查。基本类型的批注主要为string, number, bool。
接口(interface)
对象类型的类型批注。
interface Shape {readonly name: string;width: number;height?: number;}function viewShape(shape: Shape) {console.log(`The shape ${shape.name} is ${shape.width}x${shape.height}`);}viewShape({ name: 'rect', width: 30, height: 15 });
arrow函数(arrow function)
与ES6类似
类(class)
扩展了ES6中的Class,提供了public 和 private 访问修饰符。Public 成员可以在任何地方访问, private 成员只允许在类中访问。
class User {public name: string;private age: number;sex: string;constructor (name: string, age: number ) {this.name = name;this.age = age;this.sex = 'man';};shoutout() {return `The user is ${this.name}, he is ${this.age} years old.`;}}const user = new User("Alan", 17);console.log(user.shoutout());console.log(user.name);console.log(user.sex);
第二个Demo-TS与react、webpack一起使用
参考文档TS配置
创建目录
proj/├─ dist/└─ src/└─ components/
初始化、安装依赖
npm init -ynpm install --save-dev webpack webpack-clinpm install --save react react-domnpm install --save-dev @types/react @types/react-domnpm install --save-dev typescript ts-loader source-map-loader
根目录下创建ts配置文件tsconfig.json
{"compilerOptions": {"outDir": "./dist/","sourceMap": true,"noImplicitAny": true,"module": "commonjs","target": "es6","jsx": "react"}}
根目录下创建webpack配置文件webpack.config.js
module.exports = {mode: "production",devtool: "source-map",resolve: {extensions: [".ts", ".tsx"]},module: {rules: [{test: /\.ts(x?)$/,exclude: /node_modules/,use: [{loader: "ts-loader"}]},{enforce: "pre",test: /\.js$/,loader: "source-map-loader"}]},externals: {"react": "React","react-dom": "ReactDOM"}};
在src/components下创建文件
hello.tsx
import * as React from "react";export interface HelloProps { compiler: string; framework: string; }// 'HelloProps' describes the shape of props.// State is never set so we use the '{}' type.export class Hello extends React.Component<HelloProps, {}> {render() {return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;}}
index.tsx
import * as React from "react";import * as ReactDOM from "react-dom";import { Hello } from "./components/Hello";ReactDOM.render(<Hello compiler="TypeScript" framework="React" />,document.getElementById("example"));
在根目录下创建index.html
<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>Hello React!</title></head><body><div id="example"></div><!-- Dependencies --><script src="./node_modules/react/umd/react.development.js"></script><script src="./node_modules/react-dom/umd/react-dom.development.js"></script><!-- Main --><script src="./dist/main.js"></script></body></html>
在终端中运行 npx webpack
把index.html在浏览器中打开,现在应该能看到:

