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

Type inference does not work correctly with generic #32019

Closed
JounQin opened this issue Jun 21, 2019 · 2 comments
Closed

Type inference does not work correctly with generic #32019

JounQin opened this issue Jun 21, 2019 · 2 comments

Comments

@JounQin
Copy link

@JounQin JounQin commented Jun 21, 2019

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:
Type inference

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
export type AnyArray<T> = T[] | ReadonlyArray<T>;

export type Arrayable<T, R extends boolean = false> =
  | T
  | (R extends never
      ? T[]
      : R extends true
      ? ReadonlyArray<T>
      : R extends false
      ? AnyArray<T>
      : never);

export const test: Arrayable<string, never> = ['']

Expected behavior:
No error

Actual behavior:
error TS2322: Type 'string[]' is not assignable to type 'string'.

Playground Link:
playground

Related Issues:

@ahejlsberg

This comment has been minimized.

Copy link
Member

@ahejlsberg ahejlsberg commented Jun 21, 2019

This is working as intended. The conditional type (R extends never ? ... ) is a distributive conditional type, and instantiation of a distributive conditional type for the empty union never always produces never. If you don't want a distributive type, write ([R] extends [never] ? ... ) and it behaves as you expect.

@JounQin

This comment has been minimized.

Copy link
Author

@JounQin JounQin commented Jun 22, 2019

Thanks for your reply.

@JounQin JounQin closed this Jun 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.