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

154
Views
Cadena en minúsculas de TypeScript

¿Hay alguna manera de asegurarse de que TS grite si recibe algo más de una cadena en minúsculas para fieldName ?

 type Props = { onEdit: (data: Record<string, string | number>) => void; title: string; fields: { fieldName: string; fieldValue?: string | number; } };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Es bastante simple crear una utilidad para hacer cumplir que los literales de cadena estén en minúsculas usando la utilidad de tipo Lowercase<StringType> :

Parque infantil TS

 type EnforceLowerCase<T extends string> = Lowercase<T> extends T ? string extends T ? never : T : never; type Str1 = EnforceLowerCase<'Hello'>; // never type Str2 = EnforceLowerCase<'hello'>; // "hello"

Sin embargo, usarlo en su escenario sería muy complicado, porque requeriría usar solo literales de cadena (que TS no infiere en los valores de propiedad del objeto).


En su lugar, es mejor garantizar el cumplimiento de las expectativas en tiempo de ejecución. Aquí hay un ejemplo que usa una función pura que no muta el objeto de entrada:

Parque infantil TS

 function ensureLowerCaseFieldName (props: Props): Props { return {...props, fields: { ...props.fields, fieldName: props.fields.fieldName.toLocaleLowerCase(), }}; } function doSomethingWithProps (props: Props) { props = ensureLowerCaseFieldName(props); console.log(props); } const props: Props = { onEdit: data => {}, title: 'title', fields: {fieldName: 'Hello'}, }; doSomethingWithProps(props); // {..., fields: { fieldName: "hello" } }
about 4 years ago · Juan Pablo Isaza 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!