1

There is a problem in this area

if (components[i] == **TextOptionType**) {

I'm self-debugging a plug-in for a program called obsidian.

~ObsidianDevLibrary.ts is located at Importing ~type.ts.

There is a problem in referring to TextOptionType as a value.

How can I solve this?

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]))
    }
}

Maybe comparing TextOptionType with if is wrong grammar, but I don't know the right way.

It may be intended to verify that the data entering the component is formatted

https://github.com/KjellConnelly/obsidian-dev-tools

2
  • Use a type-predicate (also known as a "type-guard"): typescriptlang.org/docs/handbook/2/… Commented Jun 18, 2022 at 1:09
  • What is the type of components? Commented Jun 18, 2022 at 1:19

1 Answer 1

1

Define a type-predicate function that checks for known members of TextOptionType, like so:

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' )
    );
}

Used like so:

for( const c of components ) {
    if( isTextOptionType( c ) ) {
        componentsToReturn.push( this.addText( setting, c ) );
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. We solved the problem. I solved the problem, but when I checked, the development was stopped, so nothing was working inside. Ha ha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.