microsoft / microsoft/TypeScript

Strange behavior of types while using nested mapped types and generics

Open
#54,775 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided TypeScript Playground link and the nested object-builder example, comparing the inferred type of test with the standalone n and p declarations. Trace how nested generic calls infer OptionalyProped types; done means the nested n properties consistently infer OptionalyProped<number> and the p properties retain OptionalyProped<21>.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.