Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
Property does not exist on type error when the property does exist

I've received the aforementioned error when making the following TypeScript code. The problem is obviously that the urls, type and sub properties don't exist on a parameter of the string[] type, but they would not be read if that was the type of the targeted parameter. I'm extremely new to TypeScript, this is my first attempt working with it, so I'm probably missing something relatively basic.

interface targeterInterface {
    urls: string | string[];
    type?: string;
    sub?: string;
}
function exp(targeter: string | targeterInterface | string[]) {
    let target: string[] | string;
    let type: string;
    let sub: string;
    if (typeof targeter === "object") {
        target = targeter.urls;
        type = targeter.type;
        sub = targeter.sub;
    } else if (typeof targeter === "string") {
        target = targeter;
    } else {
        console.log("It's an array!");
    }
}

Thanks for any help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If it is not a string or an array, otherwise it is an object

if (Array.isArray(targeter)) {
    // Array
} else if (typeof targeter === "string") {
    // string
} else {
    // Instance of targeterInterface
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a TypeScript feature called type-guard function to check what is the type of the argument. https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards

interface targeterInterface {
    urls: string | string[];
    type: string; // had to change since, the type in the function is not optional
    sub: string; // had to change since, the sub in the function is not optional
}

function isTargeterInterface(item: unknown): item is targeterInterface  {
    return (item as targeterInterface).type !== undefined;
} // this is a type guard function

function exp(targeter: string | targeterInterface | string[]) {
    let target: string[] | string;
    let type: string;
    let sub: string;
    if (isTargeterInterface(targeter)) { // calling a function to check if it implements interface provided above
        target = targeter.urls;
        type = targeter.type;
        sub = targeter.sub;
    } else if (typeof targeter === "string") {
        target = targeter;
    } else {
        console.log("It's an array!");
    }
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda