Skip to content

TypeScript Note

Todo#

  • [ ] merge from dev diary TypeScript 1004 2021-10-12

class setter#

  • @2021-12-12
  • Setter will not set value. It's like declaring a method rather than a real property of the class. setter -MDN

"OneOf" Operator#

  • @2021-12-12
  • I want to make a number-like type N, such that the N can be either always number or always bigint. But it seems not possible to achieve with current TypeScript 4.5.3.

relate array and literal type#

Array.prototype.at method#

  • @2021-12-11
  • Support Array.prototype.at would come with ES2022 lib (from GitHub).
  • @2022-01-03
  • Supported by TS4.5.4

Firebase-js-SDK#

  • @2021-10-12
  • firebase-js-sdk PR#5600
  • In the context of a type for property of an interface, extends cannot be used. The &(intersection) operator has same effect.
  • There is not requirement in TypeScript for an exact match between the implementing class field and the interface field.(origin)
  • @2021-12-14
  • Read Type theory: academic study of a formal system in which every term has a "type". Natural language is has a simpler(more concrete, less abstract) grammar than formal language.

Get static types#

  • @2021-10-14
  • This is like excluding a literal type from the prototype.
  • Use utility types Exclude<>
  • With Exclude<keyof typeof CUSTOMIZED_CLASS, "prototype"> can get all static properties. here is new for me.

Module#

Typing for ... in loop#

Use file API#

Writing produces type intersection#

  • @2022-01-03
  • The problem was encountered during the refactor process of AbstrLabs.
  • Writing to a number|string type will cause the type narrow to never by intersection.
  • Details:

Type equality#

Destructuring assignment with condition#

  • @2022-01-11
  • Writing declare var then change it is not a good pattern.
  • Use var _tmp for the condition like if, switch, then assign from _tmp

ES2021 New Features#

  • @2022-02-04
  • Also called ES12.
  • String.prototype.replaceAll()
  • WeakRef and FinalizationRegistry
  • Promise.any() possibly throws an AggregateError
  • Logical Assignment Operator &&, || and ??. In the original webpage, it's not explained very clearly. x&&=y is same as x=x&&y.
  • Underscores as Numeric Separator. Like Python 1_000_000
  • All from Backbencher.dev