参考文档ts文档

安装
npm -g intsall typescript

第一个demo-基本语法特性

touch hello.ts

  1. function sayHello(name: string, word?: string) {
  2. console.log(`${name} says ${word || 'hello'}`);
  3. }
  4. sayHello('Alan');
  5. 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)

对象类型的类型批注。

  1. interface Shape {
  2. readonly name: string;
  3. width: number;
  4. height?: number;
  5. }
  6. function viewShape(shape: Shape) {
  7. console.log(`The shape ${shape.name} is ${shape.width}x${shape.height}`);
  8. }
  9. viewShape({ name: 'rect', width: 30, height: 15 });

arrow函数(arrow function)

与ES6类似

类(class)

扩展了ES6中的Class,提供了public 和 private 访问修饰符。Public 成员可以在任何地方访问, private 成员只允许在类中访问。

  1. class User {
  2. public name: string;
  3. private age: number;
  4. sex: string;
  5. constructor (name: string, age: number ) {
  6. this.name = name;
  7. this.age = age;
  8. this.sex = 'man';
  9. };
  10. shoutout() {
  11. return `The user is ${this.name}, he is ${this.age} years old.`;
  12. }
  13. }
  14. const user = new User("Alan", 17);
  15. console.log(user.shoutout());
  16. console.log(user.name);
  17. console.log(user.sex);

第二个Demo-TS与react、webpack一起使用

参考文档TS配置
创建目录

  1. proj/
  2. ├─ dist/
  3. └─ src/
  4. └─ components/

初始化、安装依赖

  1. npm init -y
  2. npm install --save-dev webpack webpack-cli
  3. npm install --save react react-dom
  4. npm install --save-dev @types/react @types/react-dom
  5. npm install --save-dev typescript ts-loader source-map-loader

根目录下创建ts配置文件tsconfig.json

  1. {
  2. "compilerOptions": {
  3. "outDir": "./dist/",
  4. "sourceMap": true,
  5. "noImplicitAny": true,
  6. "module": "commonjs",
  7. "target": "es6",
  8. "jsx": "react"
  9. }
  10. }

根目录下创建webpack配置文件webpack.config.js

  1. module.exports = {
  2. mode: "production",
  3. devtool: "source-map",
  4. resolve: {
  5. extensions: [".ts", ".tsx"]
  6. },
  7. module: {
  8. rules: [
  9. {
  10. test: /\.ts(x?)$/,
  11. exclude: /node_modules/,
  12. use: [
  13. {
  14. loader: "ts-loader"
  15. }
  16. ]
  17. },
  18. {
  19. enforce: "pre",
  20. test: /\.js$/,
  21. loader: "source-map-loader"
  22. }
  23. ]
  24. },
  25. externals: {
  26. "react": "React",
  27. "react-dom": "ReactDOM"
  28. }
  29. };

在src/components下创建文件
hello.tsx

  1. import * as React from "react";
  2. export interface HelloProps { compiler: string; framework: string; }
  3. // 'HelloProps' describes the shape of props.
  4. // State is never set so we use the '{}' type.
  5. export class Hello extends React.Component<HelloProps, {}> {
  6. render() {
  7. return <h1>Hello from {this.props.compiler} and {this.props.framework}!</h1>;
  8. }
  9. }

index.tsx

  1. import * as React from "react";
  2. import * as ReactDOM from "react-dom";
  3. import { Hello } from "./components/Hello";
  4. ReactDOM.render(
  5. <Hello compiler="TypeScript" framework="React" />,
  6. document.getElementById("example")
  7. );

在根目录下创建index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Hello React!</title>
  6. </head>
  7. <body>
  8. <div id="example"></div>
  9. <!-- Dependencies -->
  10. <script src="./node_modules/react/umd/react.development.js"></script>
  11. <script src="./node_modules/react-dom/umd/react-dom.development.js"></script>
  12. <!-- Main -->
  13. <script src="./dist/main.js"></script>
  14. </body>
  15. </html>

在终端中运行 npx webpack

把index.html在浏览器中打开,现在应该能看到:

屏幕快照 2019-11-12 23.41.00.png