typescript - TypeScript 方括号的名称?
问题描述
我正在阅读语言服务器协议(LSP)规范,我发现了这个定义:
export interface WorkspaceEdit {
/**
* Holds changes to existing resources.
*/
changes?: { [uri: string]: TextEdit[]; };
/**
* An array of `TextDocumentEdit`s to express changes to n different text documents
* where each text document edit addresses a specific version of a text document.
* Whether a client supports versioned document edits is expressed via
* `WorkspaceClientCapabilities.workspaceEdit.documentChanges`.
*/
documentChanges?: TextDocumentEdit[];
}
在changes
现场,这是一个错字,还是什么意思?
您能否向不了解 TypeScript 并且只是尝试用另一种编程语言实现消息的人解释一下?
解决方案
{ [uri: string]: TextEdit[]; };
定义了一个类型,它可以被索引string
并且索引返回TextEdit[]
。uri
只是给 index 参数的名称。
前任:
interface TextEdit { TextEdit : true } /// dummy
export interface WorkspaceEdit {
/**
* Holds changes to existing resources.
*/
changes?: { [uri: string]: TextEdit[]; };
}
let w: WorkspaceEdit;
let t = w.changes['test'] // t is of type TextEdit[]
let f: number[] = w.changes['test'] // error can't assign TextEdit[] to number[]
w.changes['test'] = "" // also error since we are assigning a string to the object
游乐场链接
推荐阅读
- c# - 其他属性依赖于它的值的惰性 getter 属性的最佳方法
- javascript - 滚动时的粘性标题并在我向上滚动时显示
- python - 使用 Python 的请求使用 WebKitFormBoundary 重新创建 POST 请求
- sphinx - Sphinx:sphinxql 中的 OR 运算符
- python - 套接字超时后提供了无效参数
- javascript - 1:1 错误解析错误:意外字符“�”
- java - 使用 Android 中的 uid 从 Firebase 获取用户节点?
- javascript - 在 Fabric js 中加载后,我们如何更改画布宽度高度
- d - 链接器错误编译 D 模板
- vba - VBA 中的应用程序定义错误