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

245
Visualizações
Is there a type for things that are instance of Object?

I'm writing a little type guarding tool, and am trying to come up with a way to split every possible type into a few types that cover every possible type, making sure there's no overlap.

I know in JavaScript primitive types are a thing, so maybe I could start with primitive and non-primitive types. Primitives are string, number, bigint, boolean, symbol, null and undefined, so I can define a type guard like:

type Primitive = string | number | bigint | boolean | symbol | undefined | null;

function isPrimitive(val: unknown): val is Primitive {
    return typeof val === 'string' 
        || typeof val === 'number' 
        || typeof val === 'bigint' 
        || typeof val === 'boolean' 
        || typeof val === 'symbol' 
        || val === undefined 
        || val === null;
}

Now I want the complement of that. On the JavaScript side, I know typeof val === 'object' won't help because that'd include null. But val instanceof Object seems to work. It returns false for everything isPrimitive returns true, and it seems to return true for everything else:

function isNotPrimitive(val: unknown): val is Object {
    return val instanceof Object;
}

The problem is that val is Object is broader than what the function returns because the TypeScript Object type seems to include primitives:

type NumberIsObject = number extends Object ? 'Yes' : 'No'; // "Yes"

So is there a type in TypeScript that corresponds to things that are instances of Object? Or if not, is there a better way to group types into broad categories that will satisfy my criteria?

about 4 years ago · Juan Pablo Isaza
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