Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

380
Views
How do I check the type of a variable inside an `if` statement

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 ) );
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!