差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
| typescript [2026/10/09 16:03] – 调整练习 2 标题层级 张叶安 | typescript [2026/11/10 17:30] (当前版本) – 张叶安 | ||
|---|---|---|---|
| 行 28: | 行 28: | ||
| - [[typescript: | - [[typescript: | ||
| - [[typescript: | - [[typescript: | ||
| - | + | | |
| - | ---- | + | |
| - | + | - [[typescript:什么是use_strict]] | |
| - | ===== 附:ES 模块导入 | + | |
| - | + | | |
| - | 第十二章讲的是 namespace(命名空间);现代项目里组织代码用的是 **ES 模块**,也就是下面这套 `export` / `import` 语法。`import { useEffect, useState } from " | + | |
| - | + | | |
| - | ==== 1. 导出(export) ==== | + | |
| - | + | - [[typescript:文本操作]] | |
| - | <code typescript> | + | |
| - | // 命名导出:一个文件可以有多个 | + | |
| - | export const PI = 3.14; | + | |
| - | export function add(a: number, b: number): number { return a + b; } | + | |
| - | export class User { | + | |
| - | | + | |
| - | } | + | |
| - | export interface Point { x: number; y: number; } // 类型也能导出 | + | |
| - | + | ||
| - | // 也可以先定义、后集中导出 | + | |
| - | const version = " | + | |
| - | interface Config { debug: boolean; } | + | |
| - | export { version, Config }; | + | |
| - | export { version as appVersion }; // 重命名导出 | + | |
| - | + | ||
| - | // 默认导出:一个模块只能有一个 | + | |
| - | export default class Logger {} | + | |
| - | // 或者 | + | |
| - | export default function main() {} | + | |
| - | + | ||
| - | // 只导出类型(编译后不产生任何代码) | + | |
| - | export type { Point }; | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 2. 导入(import) ==== | + | |
| - | + | ||
| - | < | + | |
| - | import { PI, add } from " | + | |
| - | import { add as plus } from " | + | |
| - | import Logger from " | + | |
| - | import Logger, { PI } from " | + | |
| - | import * as math from " | + | |
| - | import " | + | |
| - | import { useState, useEffect } from " | + | |
| - | </ | + | |
| - | + | ||
| - | **TypeScript 专有:类型导入** | + | |
| - | + | ||
| - | <code typescript> | + | |
| - | import type { Point, Config } from " | + | |
| - | import { type Point, add } from " | + | |
| - | </ | + | |
| - | + | ||
| - | **动态导入**(返回 Promise,常用于按需加载 / 代码分割) | + | |
| - | + | ||
| - | <code typescript> | + | |
| - | const { default: Logger } = await import(" | + | |
| - | const math = await import(" | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 3. 重新导出(转发) ==== | + | |
| - | + | ||
| - | <code typescript> | + | |
| - | // index.ts 里做“统一出口”很常见 | + | |
| - | export { add } from " | + | |
| - | export { add as plus } from " | + | |
| - | export * from " | + | |
| - | export * as math from " | + | |
| - | export type { Point } from " | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 4. 与 CommonJS 的关系 ==== | + | |
| - | + | ||
| - | Node 老项目用的是 CommonJS,写法不同,**不要和 `import/ | + | |
| - | + | ||
| - | < | + | |
| - | // CommonJS | + | |
| - | const fs = require(" | + | |
| - | module.exports = { add }; | + | |
| - | + | ||
| - | // 在 TypeScript 中使用 CommonJS 模块(配合 " | + | |
| - | import fs = require(" | + | |
| - | export = myModule; | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 5. 注意事项 ==== | + | |
| - | + | ||
| - | * `import` / `export` 必须写在**模块顶层**,不能放进函数或 `if` 里(`import()` 动态导入除外)。 | + | |
| - | * `import` 会被提升,语句写在哪不影响执行顺序,但习惯上统一放文件顶部。 | + | |
| - | * 文件里只要有顶层 `import` 或 `export`,它就是模块,变量不再污染全局。 | + | |
| - | | + | |
| - | | + | |
| - | * 默认导出和命名导出可以同时用,但一个模块只能有一个默认导出。 | + | |
| - | + | ||
| - | ==== 6. 练习题 ==== | + | |
| - | + | ||
| - | ==== 练习 1 ==== | + | |
| - | + | ||
| - | 把下面这个文件改写成「既提供命名导出、又提供默认导出」的模块,并在另一个文件里用三种方式导入(命名、默认、整体)。 | + | |
| - | + | ||
| - | < | + | |
| - | function formatDate(date: Date): string { | + | |
| - | | + | |
| - | } | + | |
| - | + | ||
| - | class Formatter { | + | |
| - | | + | |
| - | | + | |
| - | return formatDate(date); | + | |
| - | } | + | |
| - | } | + | |
| - | </ | + | |
| - | + | ||
| - | 参考答案 | + | |
| - | + | ||
| - | < | + | |
| - | // utils/ | + | |
| - | export function formatDate(date: Date): string { | + | |
| - | return date.toISOString().split(" | + | |
| - | } | + | |
| - | + | ||
| - | export class Formatter { | + | |
| - | | + | |
| - | format(date: | + | |
| - | return formatDate(date); | + | |
| - | } | + | |
| - | } | + | |
| - | + | ||
| - | export default formatDate; | + | |
| - | + | ||
| - | // main.ts | + | |
| - | import formatDate, { formatDate as named, Formatter } from " | + | |
| - | import * as formatter from " | + | |
| - | + | ||
| - | console.log(formatDate(new Date())); | + | |
| - | console.log(named(new Date())); | + | |
| - | console.log(formatter.formatDate(new Date())); | + | |
| - | console.log(new Formatter(" | + | |
| - | </ | + | |
| - | + | ||
| - | ==== 练习 2 ==== | + | |
| - | + | ||
| - | 把 `types.ts` 拆成两个文件:`types/ | + | |
| - | + | ||
| - | 参考答案 | + | |
| - | + | ||
| - | < | + | |
| - | // types/ | + | |
| - | export interface Point { x: number; y: number; } | + | |
| - | + | ||
| - | // types/ | + | |
| - | export interface User { id: string; name: string; } | + | |
| - | + | ||
| - | // types/ | + | |
| - | export type { Point } from " | + | |
| - | export type { User } from " | + | |
| - | </ | + | |