Strange behavior of types while using nested mapped types and generics

Abierto
#54,775 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Estancado
Stack tecnológico
typescript
Área
compilers

Línea de trabajo

Comienza con el enlace proporcionado de TypeScript Playground y el ejemplo del constructor de objetos anidados, comparando el tipo inferido de test con las declaraciones independientes de n y p. Sigue cómo las llamadas genéricas anidadas infieren tipos OptionalyProped; se considera terminado cuando las propiedades n anidadas infieren consistentemente OptionalyProped<number> y las propiedades p conservan OptionalyProped<21>.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Needs Investigation

Bug Report

I got two differents typings while nesting objects using some object-builder functions and generic optional typings, keeping it quite basic.

These two typings show up in a one-in-two pattern as we dig into the nested objects.
The second one matches with the type of the object-builder.

It can be a misuse of mine so let me know ...

🔎 Search Terms
  • mapped type
  • nested
  • partial optional
  • generic
🕗 Version & Regression Information

Typescript versions 5.1.4 & 5.2.0-dev.20230625 & 4.1.6

Just discovered it, so AFAIK this is not a regression.

⏯ Playground Link

Just here

💻 Code
type OptionalyProped<OPTL extends number> = { optlValue?: OPTL }
export const optionalyProped = <OPTL extends number>(proped: OptionalyProped<OPTL>): OptionalyProped<OPTL> => proped


type ObjectOfProped<T extends { [key: string]: ObjectOrProped }> = { structure: T }
export const objectOfProped = <OBJ extends { [key: string]: ObjectOrProped }>(
    objectOf: ObjectOfProped<OBJ>
    ): ObjectOfProped<OBJ> => objectOf

type ObjectOrProped = ObjectOfProped<any> | OptionalyProped<any>

const test = objectOfProped({
	structure: {
		n: optionalyProped({}),
                p: optionalyProped({ optlValue: 21}),
		o: objectOfProped({
			structure: {
				n: optionalyProped({}),
                                p: optionalyProped({ optlValue: 21}),
				o: objectOfProped({
					structure: {
						n: optionalyProped({}),
                                                p: optionalyProped({ optlValue: 21}),
						o: objectOfProped({
							structure: {
								n: optionalyProped({}),
                                                                p: optionalyProped({ optlValue: 21}),
							},
						}),
					},
				}),
			},
		}),
	},
})

const n = optionalyProped({ }) // typed: OptionalyProped<number>
const p = optionalyProped({ optlValue: 21 }) // typed: OptionalyProped<21>

🙁 Actual behavior
typeof test = ObjectOfProped<{
    n: OptionalyProped<any>;                // first behavior
    p: OptionalyProped<number>;
    o: ObjectOfProped<{
        n: OptionalyProped<number>;         // second behavior (legit one)
        p: OptionalyProped<21>;
        o: ObjectOfProped<{
            n: OptionalyProped<any>;
            p: OptionalyProped<number>;
            o: ObjectOfProped<{
                n: OptionalyProped<number>;
                p: OptionalyProped<21>;
            }>;
        }>;
    }>;
}>
🙂 Expected behavior
typeof test = ObjectOfProped<{
    n: OptionalyProped<number>;             // always
    p: OptionalyProped<21>;
    o: ObjectOfProped<{
        n: OptionalyProped<number>;         // the same
        p: OptionalyProped<21>;
        o: ObjectOfProped<{
            n: OptionalyProped<number>;     // type
            p: OptionalyProped<21>;
            o: ObjectOfProped<{
                n: OptionalyProped<number>;
                p: OptionalyProped<21>;
            }>;
        }>;
    }>;
}>

Thanks

Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de microsoft/TypeScript

Todos los issues de microsoft/TypeScript

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.