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

260
Vistas
TS2322: Type 'boolean' is not assignable to type 'undefined'. How to assign optional properties dynamically?

I am trying to assign properties to an object to form a known interface called ParsedArguments and this is how it looks like:

import {Rules} from "../Rules/types/Rules";

export interface ParsedArguments {
    //other props
    //...
    rules?: Rules,
}

And now in another function I am trying to form an object that reads values from cli and forms an objects complies to that interface. Something like this:

private parseCliArgs(args: string[]): ParsedArguments {
        let res: ParsedArguments = {}

        for (const arg of args) {
            if (arg.startsWith('--')) {
                const {key, value} = this.getArgValues(arg)

                if (!res.rules) {
                    res.rules = {}
                }

                const rule: keyof Rules = key

                if (res.rules[rule]) {
                    //HERE I GET THE ERROR
                    //TS2322: Type 'STRING' is not assignable to type 'undefined'.
                    res.rules[rule] = value
                }
            }
        }

        return res
    }

Even if I check if the property of Rules exists, it still gives me that error. I couldn't figure out yet and hope you guys help me on this. Here is how Rules look like:

export interface Rules {
    someProp1?: boolean,
    someProp2?: number,
    //...
}

I also tried const rule = key as keyof Rules instead of const rule: keyof Rules = key, doesn't change anything.

I'd appreciate if you guys let me know if you guys need more clarification or I missed any parts. Thanks in advance guys.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You don't have compatible types in Rules and value. In Rules you have optional boolean and number (so they implicitly includes undefined) and in value you always have string.
If you will make all props in Rules as strings then they will be compatible.

See this playground to get a better view of what I mean

about 4 years ago · Juan Pablo Isaza Denunciar

0

OK here is what I come up with. I used built-in Typescript generics which is quite useful in those case. Because in order to get those dynamic results, it is not quite wise to define different types all the time. You will end up cascade of types then. See below:

let rules: Partial<Record<keyof Rules, string>> = {}

for (const arg of args) {
    if (arg.startsWith('--')) {
        const {key, value} = this.getArgValues(arg)

        if (this.isRuleKey(key)) {
            rules[key] = value
        }
    }
}

Now with this solution I don't have to create another version of Rules type. Everything works fine.

about 4 years ago · Juan Pablo Isaza Denunciar
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