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

5.0: An opaque type from other modules isn't inferred correctly #53581

Open
SeokminHong opened this issue Mar 30, 2023 · 4 comments
Open

5.0: An opaque type from other modules isn't inferred correctly #53581

SeokminHong opened this issue Mar 30, 2023 · 4 comments
Labels
Bug A bug in TypeScript Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Help Wanted You can do this
Milestone

Comments

@SeokminHong
Copy link

Bug Report

πŸ”Ž Search Terms

  • unique symbol
  • type inference
  • opaque

πŸ•— Version & Regression Information

  • This changed between versions v4.9.5 and v5.0.2
  • The nightly build also has the bug

⏯ Playground Link

Playground (v5.0.2)

πŸ’» Code

declare const tag: unique symbol;
type Tagged<Token> = {
  readonly [tag]?: Token;
};
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;
export type Query<_V, _R> = Opaque<string>;

const q: Query<{}, {}> = 'query {}'

The opaque type that I borrowed from:

πŸ™ Actual behavior

The type of q is annotated correctly, Query<{}, {}> while the declaration of Query is on the same file. However, when I moved it to another file, the q is inferred as Query<_V, _R>. It is not reproduced on v4.9.5.

πŸ™‚ Expected behavior

The type of q should be inferred as Query<{}, {}> wherever the declaration of Query is located.

@RyanCavanaugh
Copy link
Member

This looks like just a tooltip issue? The declaration emit shows {}, {} as expected.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Domain: Quick Info e.g. hover text, tool-tips, and tooltips. labels Mar 30, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 30, 2023
@SeokminHong
Copy link
Author

@RyanCavanaugh Could you try the following types?

type PickVariable<T> = T extends Query<infer V, infer _R> ? V : never;
type PickResponse<T> = T extends Query<infer _V, infer R> ? R : never;

const v: PickVariable<Query<{}, {}>>;
const r: PickResponse<Query<{}, {}>>;

The v and r is inferred as {} on 4.9 but unknown on 5.0.

@whzx5byb
Copy link

Even in the same file the behavior is bad in the nightly.

declare const tag: unique symbol;
type Tagged<Token> = {
  readonly [tag]?: Token;
};
export type Opaque<Type, Token = unknown> = Type & Tagged<Token>;
export type Query<_V, _R> = Opaque<string>;

const q: Query<{}, {}> = 'query {}'
//    ^?
// 5.1.0-dev.20230331: const q: Query<_V, _R>
// 5.0.2: const q: Query<{}, {}>

@Andarist
Copy link
Contributor

The "same file" test case provided by @whzx5byb can be bisected to: #53246

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Quick Info e.g. hover text, tool-tips, and tooltips. Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

4 participants