这个区域有问题
if (components[i] == **TextOptionType**) {
我正在自我调试一个名为obsidian的程序的插件。
~ ObsidianDevLibrary。
在引用TextOptionType作为值时有一个问题。
我该如何解决这个问题?
type.ts
export type TextOptionType = {
[propName: string] : any,
key: string,
placeholder?: string,
autoSave?: boolean,
value?: boolean,
onChange?: onChangeType,
}
ObsidianDevLibrary.ts
for (let i = 0; i < components.length; i++) {
if (components[i] == TextOptionType) {
componentsToReturn.push(this.addText(setting, components[i]))
}
}
也许比较TextOptionType与if是错误的语法,但我不知道正确的方式。
它可能用于验证进入组件的数据是否被格式化
https://github.com/KjellConnelly/obsidian-dev-tools
定义一个类型谓词function
,检查TextOptionType
的已知成员,如下所示:
function isTextOptionType( x: unknown ): x is TextOptionType {
const whatIf = x as TextOptionType;
return (
( typeof whatIf === 'object' && whatIf !== null )
&&
( typeof whatIf.key === 'string' )
&&
( typeof whatIf.placeholder === 'string' || typeof whatIf.placeholder === 'undefined' )
&&
( typeof whatIf.autoSave === 'boolean' || typeof whatIf.autoSave === 'undefined' )
&&
( typeof whatIf.value === 'boolean' || typeof whatIf.value === 'undefined' )
&&
( typeof whatIf.onChange === 'function' || typeof whatIf.onChange === 'undefined' )
);
}
像这样使用:
for( const c of components ) {
if( isTextOptionType( c ) ) {
componentsToReturn.push( this.addText( setting, c ) );
}
}