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

307
Visualizações
How not to allow unknown properties after zod transform method (TypeScript)

I have an API function that returns a user, for example. I want to parse response from the server to my client User type. Let's say e.g. I want to merge first and last name of a user into one string to use in my client code.


type User = {
    uuid: string
    email: string
    fullName: string
}

const UserSchema: z.ZodSchema<User, z.ZodTypeDef, unknown> = z.object({
    uuid: z.string().uuid(),
    email: z.string().email(),
    firstName: z.string(),
    lastName: z.string()
}).transform(response => ({
    uuid: response.uuid,
    email: response.email,
    fullName: `${response.firstName} ${response.lastName}`
}))

and that works perfectly fine from TypeScript perspective. E.g. if I try to set non string value to fullName inside transform it shows an error. BUT it allows me to add properties to the final result without showing any errors:

...}).transform(response => ({
    uuid: response.uuid,
    email: response.email,
    fullName: `${response.firstName} ${response.lastName}`,
    
    // here is an extra value that should not be here because it
    // doesn't exist in my client User type but TS allows it here
    blablabla: true,
}))

I have strict and strictNullChecks enabled in my tsconfig file. How can I make an error visible in such cases?

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