confuse of : and => (quick reference)

  1. // Simple type for a function, use =>
  2. type FnType = (arg: ArgType) => ReturnType
  3. // Every other time, use :
  4. type FnAsObjType = {
  5. (arg: ArgType): ReturnType
  6. }
  7. interface InterfaceWithFn {
  8. fn(arg: ArgType): ReturnType
  9. }
  10. const fnImplementation = (arg: ArgType): ReturnType => {
  11. /* implementation */
  12. }

Function declarations

  1. // 推断返回
  2. function sum(a: number, b: number) {
  3. return a + b
  4. }
  5. // 定义返回
  6. function sum(a: number, b: number): number {
  7. return a + b
  8. }

Function Expression 函数表达式

  1. // named function expression 命名函数
  2. const sum = function sum(a: number, b: number): number {
  3. return a + b
  4. }
  5. // annonymous function expression 匿名函数
  6. const sum = function (a: number, b: number): number {
  7. return a + b
  8. }
  9. // arrow function 箭头函数
  10. const sum = (a: number, b: number): number => {
  11. return a + b
  12. }
  13. // implicit return 隐式返回
  14. const sum = (a: number, b: number): number => a + b
  15. // implicit return of an object requires parentheses to disambiguate the curly braces 隐式返回对象,需要圆括号来消除花括号的歧义
  16. const sum = (a: number, b: number): {result: number} => ({result: a + b})

sum = (a, b) => a + b 的类型声明:

  1. const sum: (a: number, b: number) => number = (a, b) => a + b
  2. // 等价于
  3. type MathFn = (a: number, b: number) => number
  4. const sum: MathFn = (a, b) => a + b
  5. // 等价于
  6. type MathFn = {
  7. (a: number, b: number): number
  8. }
  9. const sum: MathFn = (a, b) => a + b
  10. // 上面的方式可以添加属性
  11. type MathFn = {
  12. (a: number, b: number): number
  13. operator: string
  14. }
  15. const sum: MathFn = (a, b) => a + b
  16. sum.operator = '+'
  17. // type 也可以替换为 interface
  18. interface MathFn {
  19. (a: number, b: number): number
  20. operator: string
  21. }
  22. const sum: MathFn = (a, b) => a + b
  23. sum.operator = '+'
  24. // 使用 declare function 和 declare namespace
  25. declare function MathFn(a: number, b: number): number
  26. declare namespace MathFn {
  27. let operator: '+'
  28. }
  29. const sum: typeof MathFn = (a, b) => a + b
  30. sum.operator = '+'

Optional/Default params 可选参数/默认参数

可选参数

  1. const sum = (a: number, b?: number): number => a + (b ?? 0) // sum(1)
  2. const sum = (a: number | undefined, b: number): number => (a ?? 0) + b // sum(undefined, 2)

默认参数

  1. const sum = (a: number, b: number = 0): number => a + b
  2. const sum = (a: number, b: number | undefined = 0): number => a + b
  3. type MathFn = (a: number | undefined, b: number) => number
  4. const sum: MathFn = (a = 0, b) => a + b

Rest params 剩余参数

  1. const sum = (a: number = 0, ...rest: Array<number>): number => {
  2. return rest.reduce((acc, n) => acc + n, a)
  3. }
  4. // extract the type:
  5. type MathFn = (a?: number, ...rest: Array<number>) => number
  6. const sum: MathFn = (a = 0, ...rest) => rest.reduce((acc, n) => acc + n, a)

Object properties and Methods 对象属性和方法

Classes

  1. class MathUtils {
  2. sum(a: number, b: number): number {
  3. return a + b
  4. }
  5. }
  6. class MathUtils {
  7. sum = (a: number, b: number): number => {
  8. return a + b
  9. }
  10. }
  11. interface MathUtilsInterface {
  12. sum(a: number, b: number): number
  13. }
  14. class MathUtils implements MathUtilsInterface {
  15. sum(a: number, b: number): number {
  16. return a + b
  17. }
  18. }

Modules

默认导出

  1. declare const sum: {
  2. (a: number, b: number): number
  3. operator: string
  4. }
  5. export default sum

普通导出

  1. declare const sum: {
  2. (a: number, b: number): number
  3. operator: string
  4. }
  5. export {sum}

Overloads

Define function overload types with TypeScript

  1. type asyncSumCb = (result: number) => void
  2. // define all valid function signatures
  3. function asyncSum(a: number, b: number): Promise<number>
  4. function asyncSum(a: number, b: number, cb: asyncSumCb): void
  5. // define the actual implementation
  6. // notice cb is optional
  7. // also notice that the return type is inferred, but it could be specified
  8. // as `void | Promise<number>`
  9. function asyncSum(a: number, b: number, cb?: asyncSumCb) {
  10. const result = a + b
  11. if (cb) return cb(result)
  12. else return Promise.resolve(result)
  13. }

Generators

Async

  1. const sum = async (a: number, b: number): Promise<number> => a + b
  2. async function sum(a: number, b: number): Promise<number> {
  3. return a + b
  4. }

Generics

  1. const arrayify = <Type extends unknown>(a: Type): Array<Type> => [a]

Type Guards

Assertion functions


TypeScript Function Syntaxes