Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Cómo hacer que TypeScript sepa que mi variable ya no está indefinida

Aquí está mi caso:

Estoy usando un cargador Remix que obtiene parámetros de la URL pero los parámetros se definen como string | undefined

Si la variable no está undefined , me gustaría lanzar una redirección

 export const loader: LoaderFunction = async ({ params }) => { const { myVar } = params; // myVar: string | undefined definedOrRedirect(myVar, "/"); // Checks if undefined return fetchSomething(myVar); // myVar: string | undefined };

¿Hay alguna manera de hacer que TypeScript sepa que si no arrojó myVar no está indefinido?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede hacer que throwIfUndefined sea una función de aserción para restringir el tipo de su argumento a algo que no incluya undefined en su dominio. Una función de aserción tiene un predicado de tipo de aserción de la forma asserts x is Y como su tipo de devolución, donde x es el nombre de uno de los parámetros de la función, e Y es el subtipo del tipo de typeof X al que limitamos x asumiendo que la función devuelve correctamente . Las funciones de aserción no pueden devolver un valor definido; son básicamente funciones de retorno de void .

Para throwIfUndefined , esta es la forma normal de hacerlo, como una declaración de función:

 function throwIfUndefined<T>(x: T | undefined): asserts x is T { if (typeof x === "undefined") throw new Error("OH NOEZ"); }

También puede escribirlo como una función de flecha, aunque necesita anotar explícitamente la variable con su tipo para que el análisis de flujo de control se realice correctamente:

 const throwIfUndefined: <T, >(x: T | undefined) => asserts x is T = x => { if (typeof x === "undefined") throw new Error("OH NOEZ"); }

De cualquier manera debería funcionar:

 const Index = ({ params }: { params: { myVar: string | undefined } }) => { const { myVar } = params // myVar: string | undefined // myVar.toUpperCase // <-- error, Object is possibly 'undefined' throwIfUndefined(myVar); return myVar.toUpperCase() // no error now } try { console.log(Index({ params: { myVar: Math.random() < 0.5 ? "hello" : undefined } })) // 50% "HELLO" } catch (e) { console.log(e); // 50% "OH NOEZ" }

Enlace del patio de recreo al código

over 4 years ago · Santiago Trujillo Report

0

Puede omitir la definición o la definedOrRedirect y usar una verificación explícita:

 export const loader: LoaderFunction = async ({ params }) => { const { myVar } = params; // myVar: string | undefined if (myVar == undefined) { return redirect("/"); } return fetchSomething(myVar); // myVar: string }

O puede mantener definedOrRedirect y declararlo con una afirmación como sugiere jcalz :

 function definedOrRedirect(variable, path): asserts variable is string { if (variable == undefined) { return redirect(path); } } export const loader: LoaderFunction = async ({ params }) => { const { myVar } = params; // myVar: string | undefined definedOrRedirect(myVar, "/"); return fetchSomething(myVar); // myVar: string }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!