首页 > 解决方案 > 关于字符串构建的 TypeScript 语法问题

问题描述

下面的语法在 TypeScript 中有什么作用?

export interface Config {
readonly name: string
readonly buildPath: (data?: Data) => string
readonly group: string
}

export interface Data {
  id: number
  account: string
  group: 'a' | 'b' | 'b'
}

so what does the following method do?

config.buildPath(data)
given data = Data(15, 'largeAccount', 'c')

标签: javascripttypescript

解决方案


这里的接口 Data 用于定义应该被 buildPath 函数接受的对象的格式。这称为类型特定数据。

也可以直接传递对象

{id:5,account:'largeAccount',group:'c'}

即使这样也会被接受,但为了避免任何错误,最好遵循这种方法。

buildPath只是将Data作为参数并返回字符串的函数。

config是声明buildPath函数以及其他两个变量的接口。

现在buildPath所做的是由在其背后编写逻辑的实现来决定。


推荐阅读