Repository: TypeScript
Issue: Invalid declarations emitted when using ZodObject from zod as return type of generic function
Labels: Bug,Fix Available,Rescheduled

# Bug Report

### 🔎 Search Terms

zod, ZodObject, declaration, invalid declaration emit, extends infer T, union distribution, Type 'k' cannot be used to index type 'addQuestionMarks', skipLibCheck, error 2536, generic function return type



### 🕗 Version & Regression Information

Observed in all versions starting from 4.7. Tested on 4.7.4, 4.8.4, 4.9.5, 5.0.4, 5.1.3, 5.2.0-dev20230607. Works correctly on 4.6.4.

- This changed between versions 4.6.4 and 4.7.4
- This is the behavior in every version I tried, and I reviewed the full FAQ 



### ⏯ Playground Link

The playground contains relevant simplified type definitions from [zod](https://github.com/colinhacks/zod) library in an attempt to produce a minimal reproducible example.

[Playground link with relevant code](https://www.typescriptlang.org/play?ts=4.7.4#code/CYUwxgNghgTiAEAXAngBwXAjgVwJZ2AGkRkBnAHgBV4QAPREAO2FPgHsAjAK3EQD54AXngBvAFDx4AbQDW8XI3gySbAGbxKAXQBc8bMxCqFIYDXpMWG2ZvgB+eIxAA3EDHi6ZAbjEBfWSvUtbzFQSFgEFHR4KGBgAEVsEFJEXDZGAFlYGQoJDTMGZlZOHjBEABpcgCV8i1ZlZDU84Sw8AmIyKj4xAWEABVwwGXJKkBx8E06y+EqBADJ4XtgUqAhO4NDoOCQ0BFxQRhSUTqENdfBNiJ34VWhEAuPhPaZD5HJxSVl5RXrGrV1KazeHx8YJ0VBsGCIeAbcLRDjJGBQUrwMKkVgALTYwEoO3IAHlsIhUISTlBGMgBO94HAYmkIMh4AB9NiE4mIXQEomEoFiMRgiFQmFbVEYrEAGVwDERq0oAjoBUsmOxuNloh8vP5kOh51hkQQSpx6AAguSTgbcWSKaDaOCtULLlEOFBSCA8dxeJy2YaQOQAMoACygUXltXgSsqUAA7gGgyAeqJcp8FEoAvAY+gdGnA+hrFIAETM1mEvOaIHW22CnVbPXsd2lT2E71+7MIEOFMNYiPRlvxm5QO5Mci5GLxRLJVIZLIUJ0ut0lRANxBN9NxrogjU2gXasLVq7hqMrk5U2S6BEKADmmfNxvJZY3FZRaWS8A4eAgwF9YH9IAAtlATuQABqNTtmejDnnwAAUuQuDApATrogFiAAlLoxQekWS64lSsHwWkuhKhKUorEB67AkIAiQSIPjRKwlrId4QA)



### 💻 Code

```ts
export const buildSchema = <V extends string>(version: V): 
  objectOutputType<{ version: ZodLiteral<V>; }> => 
    ({}) as any;
```

Ignore `({}) as any`, it is just to avoid writing function implementation.



### 🙁 Actual behavior

Code gets compiled into the following declaration:

```ts
export declare const buildSchema: <V extends string>(version: V) => addQuestionMarks<baseObjectOutputType<{
    version: ZodLiteral<V>;
}>, undefined extends V ? never : "version"> extends infer T ? { [k in keyof T]: addQuestionMarks<baseObjectOutputType<{
    version: ZodLiteral<V>;
}>, undefined extends V ? never : "version">[k]; } : never;
```

This declaration produces the following type error when copy-pasted back into TS playground:

```
Type 'k' cannot be used to index type 'addQuestionMarks<baseObjectOutputType<{ version: ZodLiteral<V>; }>, undefined extends V ? never : "version">'.(2536)
```



### 🙂 Expected behavior

The generated declaration should be valid.

For example, this would be a valid declaration:

```ts
export declare const buildSchema: <V extends string>(version: V) => addQuestionMarks<baseObjectOutputType<{
    version: ZodLiteral<V>;
}>, undefined extends V ? never : "version"> extends infer T ? { [k in keyof T]: T[k]; } : never;
```

4.6.4 produces declaration without `extends infer T` which is also valid:

```ts
export declare const buildSchema: <V extends string>(version: V) => { [k in keyof addQuestionMarks<baseObjectOutputType<{
    version: ZodLiteral<V>;
}>, undefined extends V ? never : "version">]: addQuestionMarks<baseObjectOutputType<{
    version: ZodLiteral<V>;
}>, undefined extends V ? never : "version">[k]; };
```



### Related issues:

- https://github.com/microsoft/TypeScript/issues/49572
- https://github.com/microsoft/TypeScript/issues/49591
- https://github.com/colinhacks/zod/issues/2260
- https://github.com/colinhacks/zod/issues/1986

Special thanks to @KaoSDlanor for posting an issue in the zod repo than inspired me to write this one and @pivstone for helping me with the investigation