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

218
Visualizações
Why can Record<string, any> equal Functions?

I was testing various typescript types till I came upon the following situation.

Why can Record<string, any> equal Functions?

type C = { [key: string]: any } // Record<string, any>;
const C0: C = undefined // error
const C1: C = null // error
const C2: C = 2 // error
const C3: C = 'hello world' // error
const C4: C = { foo: 'bar' } // ok
const C5: C = () => undefined // ok

Yet Records<string, unknown> can't?

type B = { [key: string]: unknown } // Record<string, unknown>;
const B0: B = undefined // error
const B1: B = null // error
const B2: B = 2 // error
const B3: B = 'hello world' // error
const B4: B = { foo: 'bar' } // ok
const B5: B = () => undefined // error

Typescript playground

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

0

Record<string, any> is special cased for assignability and it basically means any object type. This is explained in this GitHub issue

Normally the source type must have an index signature if the target type does, but for : any there's really nothing implied by this (every property must match any, by definition), so we made [s: string]: any a no-op for assignability reasons. This enabled some patterns that didn't work before:

function doSomething(obj: { [s: string]: any}) {
  // ...
}

const obj = { a: 1, b: 2 };
// WAS ERROR: 'obj' lacks index signature
doSomething(obj);

This creates some undesirable assignability in some cases, so we didn't apply this rule to : unknown so that you could pick which behavior you wanted.

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