首页 > 解决方案 > 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 并且只是尝试用另一种编程语言实现消息的人解释一下?

标签: 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  

游乐场链接


推荐阅读