Typescript 数据结构的类型接口
运行此代码剪贴后,我将以下数据结构打印到控制台Typescript 数据结构的类型接口,typescript,tsx,Typescript,Tsx,运行此代码剪贴后,我将以下数据结构打印到控制台 let editorDelta: DeltaStatic | undefined = editor?.getContents() console.log(editorDelta) 控制台: ops: [ { attributes: { underline: true, italic: true, color: "#000fff", background: &qu
let editorDelta: DeltaStatic | undefined = editor?.getContents()
console.log(editorDelta)
控制台:
ops: [
{
attributes: {
underline: true,
italic: true,
color: "#000fff",
background: "#fff000",
bold: true
},
insert: "this"
},
{
attributes: {
underline: true,
italic: true,
color: "#000fff",
background: "#fff000",
bold: true
},
insert: "that"
},
]
我正在尝试为此数据创建类型变量,但遇到了一些问题。下面的接口正在工作,但我想为一些更嵌套的数据定义类型,如属性和insert ext
interface DeltaStatic {
ops?: object[]
}
其他一些例子我也尝试过,但都不起作用
interface DeltaStatic {
ops?: {
attributes: {
underline: boolean,
italic: boolean,
color: string,
background: string,
bold: boolean
},
insert: string
}[]
}
interface DeltaStatic {
ops?: [
{
attributes: {
underline: boolean,
italic: boolean,
color: string,
background: string,
bold: boolean
},
insert: string
}
]
}
接口通常描述对象的形状。假设
editorDelta
是一个具有唯一属性ops
的对象,该属性具有如您所述的对象数组,则可以执行以下操作:
接口DeltaStatic{
行动:{
属性:{
下划线:布尔;
斜体:布尔;
颜色:字符串;
背景:字符串;
粗体:布尔;
};
插入:字符串;
}[];
}
记住接口使用
表示项目的结束,而不是,
类型DeltaOperation={插入?:任意,删除?:编号,保留?:编号}&可选属性;
type DeltaOperation = { insert?: any, delete?: number, retain?: number } & OptionalAttributes;
interface StringMap {
[key: string]: any;
}
interface OptionalAttributes {
attributes?: StringMap;
}
interface DeltaStatic {
ops?: DeltaOperation[];
retain(length: number, attributes?: StringMap): DeltaStatic;
delete(length: number): DeltaStatic;
filter(predicate: (op: DeltaOperation) => boolean): DeltaOperation[];
forEach(predicate: (op: DeltaOperation) => void): void;
insert(text: any, attributes?: StringMap): DeltaStatic;
map<T>(predicate: (op: DeltaOperation) => T): T[];
partition(predicate: (op: DeltaOperation) => boolean): [DeltaOperation[], DeltaOperation[]];
reduce<T>(predicate: (acc: T, curr: DeltaOperation, idx: number, arr: DeltaOperation[]) => T, initial: T): T;
chop(): DeltaStatic;
length(): number;
slice(start?: number, end?: number): DeltaStatic;
compose(other: DeltaStatic): DeltaStatic;
concat(other: DeltaStatic): DeltaStatic;
diff(other: DeltaStatic, index?: number): DeltaStatic;
eachLine(predicate: (line: DeltaStatic, attributes: StringMap, idx: number) => any, newline?: string): DeltaStatic;
transform(index: number, priority?: boolean): number;
transform(other: DeltaStatic, priority: boolean): DeltaStatic;
transformPosition(index: number, priority?: boolean): number;
}
接口字符串映射{
[键:字符串]:任意;
}
接口可选属性{
属性?:字符串映射;
}
接口DeltaStatic{
操作?:DeltaOperation[];
保留(长度:数字,属性?:StringMap):DeltaStatic;
删除(长度:编号):DeltaStatic;
过滤器(谓词:(op:DeltaOperation)=>布尔值):DeltaOperation[];
forEach(谓词:(op:DeltaOperation)=>void):void;
插入(文本:任意,属性?:StringMap):DeltaStatic;
map(谓词:(op:DeltaOperation)=>T):T[];
分区(谓词:(op:DeltaOperation)=>布尔值):[DeltaOperation[],DeltaOperation[];
reduce(谓词:(acc:T,curr:DeltaOperation,idx:number,arr:DeltaOperation[])=>T,首字母:T:T;
chop():DeltaStatic;
长度():数字;
切片(开始?:编号,结束?:编号):DeltaStatic;
撰写(其他:DeltaStatic):DeltaStatic;
concat(其他:DeltaStatic):DeltaStatic;
差异(其他:DeltaStatic,索引?:编号):DeltaStatic;
eachLine(谓词:(行:DeltaStatic,属性:StringMap,idx:number)=>any,换行?:string):DeltaStatic;
转换(索引:数字,优先级?:布尔):数字;
转换(其他:DeltaStatic,优先级:boolean):DeltaStatic;
transformPosition(索引:编号,优先级?:布尔):编号;
}
Doohh!很抱歉我的示例中出现了错误。我已经试过了,但是VSCode一点也不喜欢它。我一整天都在努力让这项工作顺利进行,我认为这项工作应该很好。但是没有用。我只是想说谢谢你的帮助,我想我需要进一步研究一下。你有什么错误?这是一个很长的“导入”类型列表(“c:/Users/*/Documents/GitRep/*/node\u modules/@types/quill/index”)。DeltaStatic | undefined”不能分配给“DeltaStatic | undefined”类型。类型“import”(“c:/Users/Christian/Documents/GitRep/lessondesigner/node_modules/@types/quill/index”)。DeltaStatic不可分配给类型“DeltaStatic”。属性“ops”的类型不兼容。类型“DeltaOperation[]| undefined”不能分配给类型“{属性:{下划线:布尔;斜体:布尔;颜色:字符串;背景:字符串;粗体:布尔;};插入:字符串;}[]| undefined”。getContents()
返回的究竟是什么?它是一个数组,还是一个具有属性ops
,其值是数组的对象?后者是一个具有ops属性和数组值Delta{ops:array(1)}