• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

TypeScript实践入门和tsconfig.json配置文件

武飞扬头像
彭世瑜
帮助1

TypeScript相比JavaScript,可以很好的享受类型标注带来的代码提示,可以在编码阶段就解决大部分语法错误,极大的提升代码阅读体验和编码体验

安装TypeScript

$ node -v
v16.14.0

# 安装
$ pnpm install typescript

# 查看版本
$ npx tsc --version
Version 4.9.5

使用tsconfig.json

# 初始化 tsconfig.json 文件
$ npx tsc --init

自动生成的配置文件

tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs", 
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

修改如下

{
  "compilerOptions": {
    "target": "ES5",
    "module": "ES6", 
    "outDir": "./dist",
    "removeComments": true,
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"]
}

我们指定了输出目录,并且移除注释

TypeScript使用示例

项目结构

$ tree
.
├── package.json
├── src
│   ├── foo.ts
│   ├── index.ts
│   └── user.ts
└── tsconfig.json

package.json

{
  "type": "module",
  "dependencies": {
    "typescript": "^4.9.5"
  }
}

foo.ts

import { User } from "./user";


/**
 * 类型标注示例:打印字符串
 * @param name
 */
export function echo(name: string) {
  console.log(name);
}


/**
 * 泛型示例:原样返回
 * @param a
 * @returns
 */
export function func<T>(a: T): T {
  return a;
}

/**
 * 打印User对象
 * @param user 
 */
export function echoUser(user: User) {
  console.log({
    id: user.id,
    age: user.age,
    name: user.name,
  });
}

学新通

user.ts

export interface User {
  id: number;
  name: string;
  age: number;
}

index.ts

// 注意:
// 此处需要写扩展名,否则执行时会报错
// 如果使用webpack则不需要后缀名
import { echo, func, echoUser } from "./foo.js";

var msg: string = "Hello World!";

(() => {
  // 调用函数
  echo(msg);

  // 调用泛型函数
  func<string>("a");

  // 传入接口参数
  let user = {
    id: 1,
    name: "Tom",
    age: 20,
  };
  echoUser(user);
})();

学新通

编译执行

# 执行
$ $ npx tsc

# 查看输出的文件
$ ls ./dist
foo.js   index.js    user.js

# 执行
$ node ./dist/index.js 
Hello World!

输出的文件
user.js

export {};

foo.js

export function echo(name) {
    console.log(name);
}
export function func(a) {
    return a;
}
export function echoUser(user) {
    console.log({
        id: user.id,
        age: user.age,
        name: user.name,
    });
}

index.js

import { echo, func, echoUser } from "./foo.js";
var msg = "Hello World!";
(function () {
    echo(msg);
    func("a");
    var user = {
        id: 1,
        name: "Tom",
        age: 20,
    };
    echoUser(user);
})();

使用webpack

项目结构

$ tree
.
├── package-lock.json
├── package.json
├── pnpm-lock.yaml
├── src
│   ├── foo.ts
│   ├── index.ts
│   └── user.ts
├── tsconfig.json
└── webpack.config.cjs

package.json

{
  "type": "module",
  "dependencies": {
    "ts-loader": "^9.4.2",
    "typescript": "^4.9.5",
    "webpack": "^5.75.0",
    "webpack-cli": "^5.0.1"
  }
}

webpack.config.cjs

"use strict";

const path = require("path");

module.exports = {
  mode: "development",

  entry: "./src/index.ts",

  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
  },

  module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: "ts-loader",
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: [".tsx", ".ts", ".js"],
  },
};

学新通
# 编译
$ npx webpack

$ node ./dist/bundle.js 
Hello World!
{ id: 1, age: 20, name: 'Tom' }

参考文档

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgghigh
系列文章
更多 icon
同类精品
更多 icon
继续加载