Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional Type Inference Bug in TS 5.x #58524

Open
fxdave opened this issue May 13, 2024 · 1 comment
Open

Conditional Type Inference Bug in TS 5.x #58524

fxdave opened this issue May 13, 2024 · 1 comment

Comments

@fxdave
Copy link

fxdave commented May 13, 2024

🔎 Search Terms

"Inference trims types", "inference excludes fields"

🕗 Version & Regression Information

  • This is not a crash
  • This changed between versions _4.9 and ___5.0
  • This changed in commit or PR _______
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAYg9nAPAFQHxQLxQN5QHYCGAthAFxQBEAZghVAL5QBkUyA3AFCiRQBCBAJxTosuAG4EANgFcyrBpy7hoyAJYATVRHXDM8iAA9gEPOoDOfQYlV4qEAVABq6APysNW9QH14SNFHJ2JR41TW0fBF0sZChDY1MLX2tbeydXeUDFKmk8AGNgVTg8KBo-VAAKXIAjQI9tYQBKHA4oKAEIYGkBYurOeg4OXKKzYDaIM2lJYABGPVLy3EIScmpaABooEjMzAgBzOQoACQgQAEI6RgJEyNwtnf3yEYEbXYZUBs52ianpgDoliBsKAAemBUAAogIBHABOQAArQyACUBQADkANRUHUcHG+Dgo0MqhGUCKUG40FRt3G9zkTxeQPoqIGQzwxK+k2AACY5ggFvhiAdShQNnc9gdjmcLh8OOyppz-gKgaCoAA5OCxKEwoA

💻 Code

type Foo<T> = { name: "foo" } & T;
type Bar<T> = { value: T };

type Tidied<T> = T extends Bar<infer V> ? Tidied_Foo<T> : T;
type Tidied_Foo<T> = T extends Foo<infer V> ? T : T;

function foo<T>(cb: Tidied<T>) {
  return cb;
}

const result1 = foo({ name: "foo", message: "Hey!" } as Foo<{ message: string }>);
result1.name; // Error: Property 'name' does not exist on type '{ message: string; }'

const result2 = foo({ name: "foo", message: "Hey!" });
result2.name; // No error

🙁 Actual behavior

packages/server/src/test.ts:13:9 - error TS2339: Property 'name' does not exist on type '{ message: string; }'.

13 result1.name; // ...
           ~~~~


Found 1 error in packages/server/src/test.ts:13

🙂 Expected behavior

no type error

Additional information about the issue

I was working on my project Cuple. I found a weird bug. I spent my day to provide a small minimal example. I have a type Tidied which is used to make a developer friendly type from the complex built response types. It worked well for some cases but it doesn't work for the case that I provided. The original version makes more sense.

@fxdave fxdave changed the title Inference trims types Conditional Type Inference Bug in TS 5.x May 14, 2024
@fxdave
Copy link
Author

fxdave commented May 20, 2024

I could reduce the code even more. I believe it's minimal now. If I reduce the code more the problem goes away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant