interface Person { firstName: string; lastName: string; sayHi: () => string; }
const learner: Person = { firstName: "蓝色", lastName: "天空网", sayHi: () => "开始学习" };
console.log(learner.firstName + learner.lastName, learner.sayHi());
interface RunOptions { program: string; commandline: string | string[] | (() => string); }
function commandText(options: RunOptions): string {
const command = options.commandline;
return typeof command === "function" ? command() : typeof command === "string" ? command : command.join(" ");
}
for (const commandline of ["hello", ["hello", "world"], () => "function result"]) console.log(commandText({ program: "mbsky", commandline }));
interface Counts { [name: string]: number; }
const counts: Counts = {}; counts["教程"] = 28; console.log(counts["教程"]);
本例需在 固定版本的TypeScript编译器,启用strict;编译产物在支持ES2022的JavaScript环境中运行 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:固定版本的TypeScript编译器,启用strict;编译产物在支持ES2022的JavaScript环境中运行
入口文件:examples/67099d05feb71810f480daea/core/core.ts
预期结果与验证条件观察提示
- 蓝色天空网、开始学习
- 三个联合分支依次hello、hello world、function result
- 初始化后的字典读取教程为28;不使用未初始化对象
请结合实际环境逐项观察。
附件与关联工程包下载
课程关联工程包(登记关联72章) · 含当前章节或实例 · mbskycom202611a325.zip