Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
How to set default value for nullable field

When i try to use default function parameter with typescript, it say 'type 'A | null' is not assignable to parameter A'.

type Pagination = {
    page: number
    pageSize: number
}

interface SomeArgument {
    title: string
    pagination: Pagination | null
}

function func (arg:SomeArgument ) {
    const {title, pagination = {page: 1, pageSize: 20} } = arg
    test(pagination)
    // --^^^^^^^^^^^ Argument of type 'Pagination | null' is not assignable to parameter 
    // of type 'Pagination'.
    // Type 'null' is not assignable to type 'Pagination'.(2345)


    return {
        title, pagination
    }
}

function test ( pagination: Pagination){
    console.log(pagination)
}

So i change it like


function func (arg:SomeArgument ) {
    let { title, pagination } = args
    if(!pagination){
     pagination = {page: 1, pageSize:20}
    }
    test(pagination)

    return {
        title, pagination
    }
}

and it work. but it make unnecessary conditional context.

any better?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda