From 922c62c3abcec6a8e243c8a516b01373676d1f9f Mon Sep 17 00:00:00 2001 From: liubasara Date: Wed, 29 May 2024 18:57:57 +0800 Subject: [PATCH] =?UTF-8?q?2024=E5=B9=B405=E6=9C=8829=E6=97=A5=2018:57:57?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...347\232\204namespace\345\222\214module.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "\346\212\200\346\234\257/\347\236\216\346\212\230\350\205\276/\344\270\200\345\217\245\350\257\235\350\247\243\351\207\212ts\344\270\255\347\232\204namespace\345\222\214module.md" diff --git "a/\346\212\200\346\234\257/\347\236\216\346\212\230\350\205\276/\344\270\200\345\217\245\350\257\235\350\247\243\351\207\212ts\344\270\255\347\232\204namespace\345\222\214module.md" "b/\346\212\200\346\234\257/\347\236\216\346\212\230\350\205\276/\344\270\200\345\217\245\350\257\235\350\247\243\351\207\212ts\344\270\255\347\232\204namespace\345\222\214module.md" new file mode 100644 index 0000000..12515b7 --- /dev/null +++ "b/\346\212\200\346\234\257/\347\236\216\346\212\230\350\205\276/\344\270\200\345\217\245\350\257\235\350\247\243\351\207\212ts\344\270\255\347\232\204namespace\345\222\214module.md" @@ -0,0 +1,26 @@ +--- +name: 一句话解释ts中的namespace和module +title: 一句话解释ts中的namespace和module +tags: ["技术", "瞎折腾"] +categories: 瞎折腾 +info: "伟大的向后兼容" +time: 2024/5/29 +desc: namespace和module区别 +keywords: ['html', 'typescript', 'namespace', 'module'] +--- + +# 一句话解释ts中的namespace和module + +> 参考资料: +> [在TypeScript中使用namespace封装数据](https://pengfeixc.com/blogs/javascript/typescript-namespace) + +- 命名空间 namespace 会被编译成l立即执行的闭包函数,是`import`和`class`出现之前的产物(这俩对应的是 ts 里的 module) +- namespace 和 enum 类似,都是同样可以用来作为值和类型的存在。namespace 用来描述类似 class 一样的结构。其唯一的区别就是引用方式。 +- module 作为值时,可以使用`import A from 'aaa'`来引入,而 namespace 作为值时,除了同样支持 module 的语法以外,还支持通过`import API_Types = MyLibA.Types; `的方式引入,这是一种用于兼容的语法糖,现在已经很少用了。 +- module 作为类型时,可以用`import type A from 'aaa'`的方式引入,而 namespace 由于天生同时是值和类型,所以也能这样用。但在 @types 的全局声明 dts 文件中,不能使用 import 语法,因为会使得该 dts 文件被识别成模块。所以这个时候可以使用`/// `的方式,将 namespace 作为单纯的类型引入。 + + + + + +伟大的向后兼容!! \ No newline at end of file