TypeScript命名空间

墨蓝 2023-07-23 09:59 2阅读 0赞

namespace 命名空间,可以把一组全局变量封装成一个统一的接口,选择性的进行暴露,实现组件化开发思想。

  1. 用 namespace Home 包裹全局变量
  2. 需要暴露的变量使用 export 导出
  3. 使用 new Home.Page() 执行
  1. // page.ts
  2. ///<reference path='./components.ts' />
  3. namespace Home {
  4. export class Page {
  5. constructor() {
  6. new Component.Header();
  7. new Component.Content();
  8. new Component.Footer();
  9. }
  10. }
  11. }
  12. // components.ts
  13. namespace Component {
  14. const common = (title: string): void => {
  15. const elem = document.createElement('div');
  16. elem.innerHTML = `This is ${ title}`;
  17. document.body.appendChild(elem);
  18. };
  19. export class Header {
  20. constructor() {
  21. common('Header');
  22. }
  23. }
  24. export class Content {
  25. constructor() {
  26. common('Content');
  27. }
  28. }
  29. export class Footer {
  30. constructor() {
  31. common('Footer');
  32. }
  33. }
  34. }
  35. <!DOCTYPE html>
  36. <html lang="en">
  37. <head>
  38. <meta charset="UTF-8">
  39. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  40. <title>Document</title>
  41. </head>
  42. <body>
  43. <script src="./dist/page.js"></script>
  44. <script> new Home.Page(); </script>
  45. </body>
  46. </html>
  47. // tsconfig.json
  48. {
  49. "compilerOptions": {
  50. /* Basic Options */
  51. // "incremental": true, /* Enable incremental compilation */
  52. "target": "es5",
  53. /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
  54. "module": "amd",
  55. /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
  56. // "lib": [], /* Specify library files to be included in the compilation. */
  57. // "allowJs": true, /* Allow javascript files to be compiled. */
  58. // "checkJs": true, /* Report errors in .js files. */
  59. // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
  60. // "declaration": true, /* Generates corresponding '.d.ts' file. */
  61. // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
  62. // "sourceMap": true, /* Generates corresponding '.map' file. */
  63. "outFile": "./dist/page.js",
  64. /* Concatenate and emit output to single file. */
  65. "outDir": "./dist",
  66. /* Redirect output structure to the directory. */
  67. "rootDir": "./src",
  68. /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
  69. // "composite": true, /* Enable project compilation */
  70. // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
  71. // "removeComments": true, /* Do not emit comments to output. */
  72. // "noEmit": true, /* Do not emit outputs. */
  73. // "importHelpers": true, /* Import emit helpers from 'tslib'. */
  74. // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
  75. // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
  76. /* Strict Type-Checking Options */
  77. "strict": true,
  78. /* Enable all strict type-checking options. */
  79. // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
  80. // "strictNullChecks": true, /* Enable strict null checks. */
  81. // "strictFunctionTypes": true, /* Enable strict checking of function types. */
  82. // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
  83. // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
  84. // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
  85. // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
  86. /* Additional Checks */
  87. // "noUnusedLocals": true, /* Report errors on unused locals. */
  88. // "noUnusedParameters": true, /* Report errors on unused parameters. */
  89. // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
  90. // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
  91. /* Module Resolution Options */
  92. // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
  93. // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
  94. // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
  95. // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
  96. // "typeRoots": [], /* List of folders to include type definitions from. */
  97. // "types": [], /* Type declaration files to be included in compilation. */
  98. // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
  99. "esModuleInterop": true,
  100. /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
  101. // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
  102. // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
  103. /* Source Map Options */
  104. // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
  105. // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
  106. // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
  107. // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
  108. /* Experimental Options */
  109. // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
  110. // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
  111. /* Advanced Options */
  112. "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
  113. }
  114. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,2人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TypeScript命名空间

    > namespace 命名空间,可以把一组全局变量封装成一个统一的接口,选择性的进行暴露,实现组件化开发思想。 > > 1. 用 namespace Home 包裹全局变

    相关 TypeScript 命名空间和模块

    //命名空间namespace //创建一个命名空间,命名空间中存放的是类,是方便我们导入命名空间来调用类或者方法的 //命名空间的好处就是,让文件中的类名等不怕重复,

    相关 命名空间

    一、为什么使用命名空间 一个大型的工程往往是由若干个人独立完成的,不同的人分别完成不同的部分,最后再组合成一个完整的程序。由于各个头文件是由不同的人设计的,有可能在不同的头