TypeScript英文笔记|青训营

105 阅读1分钟

前言

还有即将在国外投身秋招的小伙伴吗?本文是将TypeScript课堂内容翻译成英文的笔记,加入了自己Google的内容。给将要在国外面试和在国内想学习英文的小伙伴们都做个参考~

Define a class:

  • class className { attrName: Type }

Visibility/Access modifiers:

  • Private: Only accessible within its own class
  • Protected: Only accessible within its own class and its subclasses
  • Both private and protected cannot be accessed by class instances
  • Public: Anyone can access

Inheritance

  • Interfaces & "implements"

    • Interfaces can not be instantiated
    • A class can implement multiple interfaces
    • All methods in an interface are by default abstract and must be implemented by any class that implements the interface.
    • An interface can only have public access
  • Abstract classes &"extends"

    • Abstract classes cannot be instantiated
    • A class can only extend one class
    • Override: When a class extends another class, it can replace the members of the parent class with the same name.
    • In an abstract class, some methods can be implemented, while others are left abstract, meaning that they have no implementation and must be overridden by concrete subclasses.
    • An abstract class can have member variables, while an interface cannot.
    • An abstract class can have different access modifiers for its members and functions

Generics

  • Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces.

Advanced Types

  • Union types

    • let num: number | string
    • num = 8, or num = "eight"
  • Intersection types

    • interface Person {..}
    • type Student = Person & {grade: number}
  • Type casting

    • function foo(arg: number | string) { const str = arg as string}

总结

TypeScript基本上算是把JavaScript升级成Java的语言,许多东西都是互通的,包括OOP的各种原则,强类型,连许多关键词都是一样的(extends, implements, etc)。这样大家学一个就可以记住另一个了~