Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TypeScript 5.x 更新


TypeScript 由 Microsoft 积极维护和更新。在 5.x 版本中,进行了许多实用程序和提高生活质量的更新。

本章涵盖了最受欢迎的更新,以允许更严格和更灵活的类型安全。


提醒一下,这些功能仅在 5.x+ 版本中可用。

模板字面量类型

模板字面量类型 现在允许我们使用模板字面量创建更精确的类型。我们可以定义在编译时依赖字符串实际值的自定义类型。

示例

type Color = "red" | "green" | "blue";
type HexColor<T extends Color> = `#${string}`;

// 用法
let myColor: HexColor<"blue"> = "#0000FF";
自己尝试 »

索引签名标签

索引签名标签 允许我们使用计算属性名称为索引签名添加标签。它有助于在处理动态对象时提供更具描述性的类型信息。

示例

type DynamicObject = { [key: string as `dynamic_${string}`]: string };

// 用法
let obj: DynamicObject = { dynamic_key: "value" };
自己尝试 »


5.x 现在也支持原生 JavaScript 私有字段。TypeScript 的 'private' 仍然像在部分中讨论的那样工作。

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.