美文网首页
09-TypeScirpt-模块系统-命名空间

09-TypeScirpt-模块系统-命名空间

作者: 低头看云 | 来源:发表于2020-10-05 07:55 被阅读0次

模块系统

  • 1.ES6模块

  • 1.1分开导入导出

    • export xxx;
    • import {xxx} from "path";
  • 1.2一次性导入导出

    • export {xxx, yyy, zzz};
    • import {xxx, yyy, zzz} from "path";
  • 1.3默认导入导出

    • export default xxx;
    • import xxx from "path";
  • 2.Node模块

  • 2.1通过exports.xxx = xxx导出

    • 通过const xxx = require("path");导入
    • 通过const {xx, xx} = require("path");导入
  • 2.2通过module.exports.xxx = xxx导出

    • 通过const xxx = require("path");导入
    • 通过const {xx, xx} = require("path");导入
  • ES6的模块和Node的模块是不兼容的, 所以TS为了兼容两者就推出了
    • export = xxx;
    • import xxx = require('path');

命名空间

  • 命名空间可以看做是一个微型模块,
  • 当我们先把相关的业务代码写在一起, 又不想污染全局空间的时候
  namespace Validation {
    const lettersRegexp = /^[A-Za-z]+$/
    export const LettersValidator = (value) => {
      return lettersRegexp.test(value)
    }
  }

  // 可以通过 reference 来引入其他文件的命名空间

  //   /// <reference path="./56/test.ts" />
  console.log(Validation.LettersValidator('abc'))
  console.log(Validation.LettersValidator(123))

命名空间合并

  • 在 ts当中接口和命名空间 是可以重名的
  • ts会将多个同名的合并为一个
  interface TestInterface {
    name: string
  }

  interface TestInterface {
    age: number
  }

  class Person implements TestInterface {
    name: string
    age: number
  }

  // 注意点: 如果同名接口中的属性名相同, 那么属性类型必须一致
  // subsequent property declarations must have the same type.  Property 'name' must be of type 'string', but here has type 'boolean'.
  // interface TestInterface {
  //   name: boolean
  // }
  • 同名接口如果出现同名函数, 那么就会成一个函数的重载
  interface TestInterface1 {
    getValue(value: number): number
  }
  interface TestInterface1 {
    getValue(value: string): number
  }

  let obj: TestInterface1 = {
    getValue(value: any): number {
      if (typeof value === 'string') {
        return value.length
      } else {
        return value.toFixed()
      }
    },
  }
  • 命名空间合并
  namespace Test {
    export let name: string = 'css'
  }

  namespace Test {
    export let age: number = 12
  }

  console.log(Test.name)
  console.log(Test.age)
  • 同名的命名空间中不能出现同名的变量, 方法等
  namespace Test1 {
    export let name: string = 'js'
    export let age: number = 112
  }

  // 报错
  // namespace Test1 {
  //   export let name: string = 'js'
  //   export let age: number = 112
  // }

  • 同名的命名空间中其他命名空间没有通过 export到处的内容是获取不到
  namespace Test2 {
    let name: string = 'java'
  }
  namespace Test2 {
    export let say = () => {
      console.log(`name is ${name}`)
    }
  }

  Test2.say() // name is
  • 除了同名的接口和命名空间可以合并以外
  • 命名空间还可以和同名的类 / 函数/ 枚举合并

命名空间和类合并

  • 注意点: 类必须定义在命名空间的前面
  • 会将命名空间中导出的方法作为一个静态方法合并到类中
  class Person1 {
    say(): void {
      console.log('32', 32)
    }
  }
  namespace Person1 {
    export let eat = () => {
      console.log('1', 1)
    }
  }

  console.dir(Person1)
  // eat 作为一个静态方法合并到类中
  Person1.eat()
  let p = new Person1()
  p.say()

命名空间和函数合并

  • 注意点:函数必须定义在命名空间的前面
  function getCounter() {
    getCounter.count++
    console.log(getCounter.count)
  }

  namespace getCounter {
    export let count: number = 1
  }
  getCounter() // 2

命名空间和枚举合并

  • 注意点:没有先后顺序的要求
  enum Gender {
    Male,
    Female,
  }
  namespace Gender {
    export const Yao: number = 66
  }
  console.log(Gender)

相关文章

  • 09-TypeScirpt-模块系统-命名空间

    模块系统 1.ES6模块 1.1分开导入导出export xxx;import {xxx} from "path"...

  • 模块化

    1. 模块化 模块化是指把一个复杂的系统分解到多个模块以方便编码。 1.1 命名空间 开发网页要通过命名空间的方式...

  • TypeScript(四)

    六.模块(命名空间、模块) 内部模块称为命名空间;注意:命名空间里面的变量是私有的,在外部使用的话需要通过expo...

  • Vuex、modules

    模块 命名空间 我们可以给模块对象上添加namespaced: true 命名空间: 可以给我们的模块的gette...

  • typescript中的命名空间

    命名空间和模块不要混用,不要在一个模块中使用命名空间,命名空间最好在一个全局的环境中使用 namespace中可以...

  • 模块化

    模块化 模块化是指把一个复杂的系统分解到多个模块以方便编码 1. 开发网页需要通过命名空间的方式来组织代码 命名空...

  • Module

    Module 模块化 + 命名空间

  • TypeScript基础入门之命名空间和模块

    转载 # TypeScript基础入门之命名空间和模块 命名空间和模块 关于术语的说明:值得注意的是,在TypeS...

  • ThinkPHP5 模块设计

    5.0版本默认采用多模块的架构,并且支持单一模块设计,所有模块的命名空间均以app作为根命名空间(可配置更改)。 ...

  • 速撸《python学习手册》--第21-24章-模块

    第21章 模块:宏伟蓝图 为什么使用模块 代码重用 系统命名空间的划分 实现共享服务和数据 import如何工作 ...

网友评论

      本文标题:09-TypeScirpt-模块系统-命名空间

      本文链接:https://www.haomeiwen.com/subject/viguuktx.html