美文网首页
Typescript 3.8 常用新特性

Typescript 3.8 常用新特性

作者: 前端小白的摸爬滚打 | 来源:发表于2021-12-05 19:38 被阅读0次

类型限制的导入导出方法 (Type-Only Imports and Export)

TypeScript 3.8 为仅类型导入和导出添加了新语法。此时导入、导出的变量只能作为类型使用

import type { SomeThing } from "./some-module.js";

export type { SomeThing };
import type { Component } from "react";

interface ButtonProps {
    // ...
}

class Button extends Component<ButtonProps> {
    //               ~~~~~~~~~
    // error! 'Component' only refers to a type, but is being used as a value here.

    // ...
}

ECMAScript 提案的私有字段(ECMAScript Private Fields)

Private Fields 的基本特性

  • js 中已经有提案,但是浏览器中还不支持

  • typescript 中已经可以使用#来定义真正的私有属性了

class Person {
    #name: string

    constructor(name: string) {
        this.#name = name;
    }

    greet() {
        console.log(`Hello, my name is ${this.#name}!`);
    }
}

let jeremy = new Person("Jeremy Bearimy");

jeremy.#name
//     ~~~~~
// Property '#name' is not accessible outside class 'Person'
// because it has a private identifier.

⚠️ 和常规属性(这里特别比较 private 修饰符声明的比较)不同,私有字段(private fields)拥有下面这些特性。

  • 专用字段以 # 字符开头。有时我们称这些 prviate name。
  • 每个专用字段名称都唯一地限定于其包含的类。
  • TypeScript 辅助功能修饰符,例如 public,private 不能在私有字段上使用。

Private Fields 的使用规范

除了能保存自己的私有这一属性以外,私有字段的另一个好处是我们刚才提到的唯一性。例如,常规属性声明易于在子类中被覆盖。而 private fields 是受保护的。

class C {
  foo = 10;

  cHelper() {
    return this.foo;
  }
}

class D extends C {
  foo = 20;

  dHelper() {
    return this.foo;
  }
}

let instance = new D();
// 'this.foo' refers to the same property on each instance.
console.log(instance.cHelper()); // prints '20'
console.log(instance.dHelper()); // prints '20'

那我们到底该使用 # 定制的私有字段还是使用 private 修饰符?

当涉及到属性时,TypeScript 的private修饰符会并没有完全正确的执行,它的行为完全像普通属性一样,我们称之为 soft privacy, 我们依然可以通过 ['foo'] 这样的形式访问到。看下面的代码:

class C {
  private foo = 10;
}

// This is an error at compile time,
// but when TypeScript outputs .js files,
// it'll run fine and print '10'.
console.log(new C().foo); // prints '10'
//                  ~~~
// error! Property 'foo' is private and only accessible within class 'C'.

// TypeScript allows this at compile-time
// as a "work-around" to avoid the error.
console.log(new C()["foo"]); // prints '10'

对比下面使用 # 私有字段,是完全访问不到的

class C {
    #foo = 10;
}

console.log(new C().#foo); // SyntaxError
//                  ~~~~
// TypeScript reports an error *and*
// this won't work at runtime!

console.log(new C()["#foo"]); // prints undefined
//          ~~~~~~~~~~~~~~~
// TypeScript reports an error under 'noImplicitAny',
// and this prints 'undefined'.

结论就是,如果你想严格的保护您的私有属性的值,就使用 # 即可,子类继承的时候也无需担心命名冲突的问题。当我们还是使用 private 的时候就需要注意对私有修饰符的定义的值修改的问题了.

export * as xxx 语法使用

typescript 也支持这种用法啦,在导入模块的 as 重新定义模块名的模块的时候,我们可以重新导出到单独模块名。

menu.ts

export const MENU1 = "nav: 菜单 1";
export const MENU2 = "nav: 菜单 2";
export const MENU3 = "nav: 菜单 3";
export const MENU4 = "nav: 菜单 4";
export const DEMO = "nav:Demo";

index.ts

import * as menu from "./menu.ts";
console.log(menu.MENU1); // 'nav: 菜单 1';
console.log(menu.MENU2); // 'nav: 菜单 2';
// ....

export { menu };

import * as React from 'react'; vs import React from 'react'; 这两个使用有什么区别?

简而言之就是我们使用的 import React from 'react' 其实是导出的默认的模块,而用到 * as 是导出全部模块。

顶层 await 使用

js 也支持啦 😄

const response = await fetch("...");
const greeting = await response.text();
console.log(greeting);

// Make sure we're a module
export {};

注意:顶层 await 只会在模块中起作用,在非模块文件中使用会报错。顶层 await 仅在模块的顶层起作用,并且只有当 TypeScript 找到一个真正可用的模块才允许使用,我们可以用一个 export {} 来检测是否在模块下使用。

const response = await fetch("...");
const greeting = await response.text();
console.log(greeting);

// 'await' expressions are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.

相关文章

  • Typescript 3.8 常用新特性

    类型限制的导入导出方法 (Type-Only Imports and Export) TypeScript 3.8...

  • Typescript 3.7 常用新特性

    写在前面 不是完整的版本新特性,这里只写出了我觉得常用的新特性 可选链式运算符 ?. js 也支持了这个特性 ob...

  • Typescript 3.9 常用新特性

    写在前面 挑重点的讲一讲 在条件语句中检测未调用的函数 在 3.7 的时候引入了检测未调用函数错误提示,3.9 做...

  • Typescript 4.5 常用新特性

    Awaited 类型与 Promise 改进 TypeScript 4.5 引入了一种新的实用程序类型: Awai...

  • Typescript 4.4 常用新特性

    用于 Aliased 条件的控制流分析 在这个例子中,我们检查了 arg 是否是一个 string。TypeScr...

  • Typescript 4.3 常用新特性

    override 和 --noImplicitOverride 标志 当一个方法被标记为 override 时,T...

  • Typescript 4.0 常用新特性

    写在前面 并不是所有的新新特性,只是罗列一些重点的 可变元祖类型 元组类型语法中的 spread 现在可以泛型 r...

  • Typescript 4.1 常用新特性

    模版字面量类型 在替代位置有联合类型呢?它会生成可以由每个联合成员表示的所有可能的字符串字面量的集合。 映射类型中...

  • Typescript 4.2 常用新特性

    元组类型中的前导 / 中间剩余元素 每个元组只有一个剩余元素,并且剩余元素后面不能有可选元素。 这些没有后缀的剩余...

  • TypeScript项目引用(project reference

    转发 # TypeScript项目引用(project references) TypeScript新特性之项目引...

网友评论

      本文标题:Typescript 3.8 常用新特性

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