Repository: TypeScript
Issue: Strange behavior of types while using nested mapped types and generics
Labels: 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](https://www.typescriptlang.org/play?#code/C4TwDgpgBA8mwEsD2A7AhgGxABQE5MgBMAeGbAFQBkoIAPYCFQgZyhQFcBbAIwlwD4oAXigBvKAWAYAapnYQA-AC5YFagF8AUHTBJcwKAGNUzA5OToseAhELCopNTXqMWbLrwEAKMPiIq4RFRMHD9bRyp+AEoA+AsQ6yIIykEhQV8bQk1s0EhYbgArCENgGAAzRPDyZwYmVnEAbQBrCBAVU1wEFABzAF0AwuLS3Eq7dVSxKA72EvZcCBVqrR09A2MUUwlBkvLR+1IAIQApGtd6qGbW9uBOnv78op2RsLGAGlUqU7r3Hj5+L00UCBW0epTKA1BuxehyO-EBwJiDyGUMyMNSgiQ2zBOXA0BgWJgz0y9nxkIq0LQKBAggAPrA4sErBSqXDNOtNgxNiJMWTRl5RJoAJDTWbzFQCwWClAqcyM0KZfnqKKveHA4FgGUMyzyoj8iTwGRyBZQABMAEYlSrJUgZQTyQqJZKRcA5sbHZLpfqgtq+aJLaq1YG1RqvfEmQ6vYaMPIVOb-ZLrbbeS9+UKE8KbjMXWKxGn057ZT6U37lQGg+XyyHCwli5HZNHjXHS+nEyDkfbde7087XeK8y2C1qaw7-RWx+PA1Wh+HO3WjbGLc2W4L1FaW-H06v+xuV2v41uldl2QYUPZqzPbHqlVAAPQ3qC5Wyxb3DpIcX4CNkmMxn6c6y-iJIUYxqaZpQNed4PrihDPmG-4kOa-BAA)

### 💻 Code

```ts
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

```ts
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

```ts
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