javascript - 关于字符串构建的 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')
解决方案
这里的接口 Data 用于定义应该被 buildPath 函数接受的对象的格式。这称为类型特定数据。
也可以直接传递对象
{id:5,account:'largeAccount',group:'c'}
即使这样也会被接受,但为了避免任何错误,最好遵循这种方法。
buildPath只是将Data作为参数并返回字符串的函数。
config是声明buildPath函数以及其他两个变量的接口。
现在buildPath所做的是由在其背后编写逻辑的实现来决定。
推荐阅读
- typescript - 打字稿如何强制接口中特定键的类型?
- r - 如何删除R中具有NULL值的行
- sql-server - 我无法远程访问 SQL Server
- html - Esp32 和站点通信
- javascript - 使用 node.js 评估 javascript 源代码模块的最佳方法
- python - “找不到模块”python虚拟环境
- linux - 如何在不更新克隆它的本地存储库的情况下设置 git 存储库的远程 url?
- tcl - TCL - 尝试并行执行 proc 但出现奇怪的 Java 错误
- linux - 如何查看没有文件类型的可执行文件
- python - NOT NULL 约束失败:KNB_balance.user_id Django