ts 3.8

仅类型的导入和导出

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

export type { SomeThing };

ECMAScript私有字段

private属性与其他任何属性都没有不同,因此无论您以哪个运行时为目标,访问它们的速度都与其他任何属性一样快。相反,由于#专用字段是使用WeakMaps 降级的,因此使用它们的速度可能较慢。尽管某些运行时可能会优化其#私有字段的实际实现,甚至具有快速的WeakMap实现,但并非在所有运行时中都如此。

class Person {
  // 必须先声明私有字段
  #name: string = "ajanuw";
  name = "suou";

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

  hello() {
    console.log(`hello ${this.#name}`);
  }
}

let p = new Person();
console.log(p); // Person { name: 'suou' }
p.hello(); // hello ajanuw

export * as ns Syntax

// util/u.ts

export function a() {
  console.log("a");
}

export function b() {
  console.log("b");
}
// util/index.ts

export * as util from "./u";

// 之前
// import * as utilities from "./utilities.js";
// export { utilities };
// index.ts

import { util } from "./util";

util.a(); // a
util.b(); // b

顶层 await

配置 "target": "es2017" 和 "module": "esnext"。 顶层await可能无法在您可能期望的所有环境中都起作用

let a = await Promise.resolve(1);
console.log(a);
console.log(2);

export {};

JSDoc属性修饰符

TypeScript 3.8通过打开allowJs标志来支持JavaScript文件,并且还支持通过选项或在文件顶部添加注释来对这些JavaScript文件进行类型检查@ts-check

访问修饰符:@public@private,和@protected, @readonly

// @ts-check

class Foo {
  constructor() {
    /** @private */
    this.stuff = 100;
  }

  printStuff() {
    console.log(this.stuff);
  }
}

new Foo().stuff; // 错误!属性“stuff”为私有属性,只能在类“Foo”中访问。ts

猜你喜欢

转载自www.cnblogs.com/ajanuw/p/12579300.html
3.8
ts