当前位置:   article > 正文

ts3.4版本特性_typesript 3和4qubie

typesript 3和4qubie

ts3.4版本特性

Faster subsequent builds with the--incrementalflag

--incremental其实是和--tsBuildInfoFile联合起来使用的,--incremental是为我们上一次的compile信息做一次硬缓存,在我们下一次修改项目时会进行信息比对然后减少编译的消耗。--tsBuildInfoFile指定我们的缓存文件的目录,相对于cmd shell的相对路径吧。

同时--outFile也会影响--tsBuildInfoFile的文件位置。

具体链接:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#composite-projects

Higher order type inference from generic functions(泛型函数加入的高阶函数推导)

高阶函数的定义是:函数的参数为另一个函数。

interface Box<T> {
  value: T;
}
function makeArray<T>(x: T): T[] {
  return [x];
}
function makeBox<U>(value: U): Box<U> {
  return { value };
}
function compose<A, B, C>(f: (arg: A) => B, g: (arg: B) => C): (arg: A) => C {
  return (x) => g(f(x));
}
//这里的compose B的推导是A的返回值类型, 所以 B 传入 T[]
const makeBoxedArray = compose(makeArray, makeBox);
makeBoxedArray("hello!").value[0].toUpperCase();

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
Improvements forReadonlyArrayandreadonlytuples

只读数组声明

1.内置lib ReadonlyArray<string>
2.type myReadonly<T> = readonly T[]
3.错误申明 type aaa = readonly Array<boolean>;  //第三个声明方式是不行的
  • 1
  • 2
  • 3

利用映射类型声明readonly/writable数组,这是类型更加灵活

type mappedReadonlyArr<T> = {
    +(-) readonly[K in keyof T]: T[K]
}
  • 1
  • 2
  • 3
constassertions

const声明直接将变量声明,同时进行类型声明转换为只读类型,使用const声明推导出的字面量类型不会转为更加泛用的类型。

const a = "1" //type "1"
let a1= "1" //type string
let a2 = "1"  as const //type "1"
let a3 = <const> "1" //type "1" 该<>的方式不能在.tsx中使用,很好理解.tsx文件中有很多标签<div>....
  • 1
  • 2
  • 3
  • 4

使用const模拟enum

export const Colors = {
  red: "RED",
  blue: "BLUE",
  green: "GREEN",
} as const;
  • 1
  • 2
  • 3
  • 4
  • 5
Type-checking forglobalThis

相当于为global提供一定的类型声明

// in a global file:
var abc = 100;
// Refers to 'abc' from above.
globalThis.abc = 200;
  • 1
  • 2
  • 3
  • 4

let,const声明的变量不会被添加到globalThis

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/255297
推荐阅读
相关标签
  

闽ICP备14008679号