Skip to content

Objects with the same type from different files have different behavior. #46834

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

Closed
TotooriaHyperion opened this issue Nov 17, 2021 · 2 comments
Closed

Comments

@TotooriaHyperion
Copy link

TotooriaHyperion commented Nov 17, 2021

Bug Report

Objects with the same type from different files have different behavior.

It happens when the type extends a type that is not Empty.

🔎 Search Terms

Actually, the problem is so weird that as a non-native English speaker, I don't know what keyword I should use to search.

I can only find something like duplicated type declarations from multiple files etc...

🕗 Version & Regression Information

typescript: 4.4.4
tslib: 2.3.1

I just find that inconsistent behavior.

Add: Just checked all my recently used versions, this happens from(but not starts at, because I haven't used this code at versions that are lower than 4.1.3) [email protected] to [email protected].

⏯ Playground Link

It only happens in a situation with multiple files. So I have to reproduce it with a repo.

https://github.com/TotooriaHyperion/maybe-ts-bug

💻 Code

https://github.com/TotooriaHyperion/maybe-ts-bug

image

// We can quickly address your report if:
//  - The code sample is short. Nearly all TypeScript bugs can be demonstrated in 20-30 lines of code!
//  - It doesn't use external libraries. These are often issues with the type definitions rather than TypeScript bugs.
//  - The incorrectness of the behavior is readily apparent from reading the sample.
// Reports are slower to investigate if:
//  - We have to pare too much extraneous code.
//  - We have to clone a large repo and validate that the problem isn't elsewhere.
//  - The sample is confusing or doesn't clearly demonstrate what's wrong.

🙁 Actual behavior

Notice that in src/spec.ts, Line 25 to 35
The infer2(outerIdWithSymbol) should not error but behaves the same as infer2(innerIdWithSymbol).

// changing 
export interface ToInferWithSymbol<T = any> extends Symbol {}
// to
export interface Empty {}
export interface ToInferWithSymbol<T = any> extends Empty {}
// can fix it

// but this also has the problem
export interface NotEmpty {
  a?: number;
}
export interface ToInferWithSymbol<T = any> extends NotEmpty {}

🙂 Expected behavior

The two object(innerIdWithSymbol and outerIdWithSymbol) from different folder with same type Identifier<Target> behaves consistently.

@TotooriaHyperion TotooriaHyperion changed the title Objects with the same type from different folders have different behavior. Objects with the same type from different files have different behavior. Nov 17, 2021
@whzx5byb
Copy link

If you change

const innerIdWithSymbol: IdentifierWithSymbol<Target> = Symbol("");

to

const innerIdWithSymbol: IdentifierWithSymbol<Target> = Symbol("") as IdentifierWithSymbol<Target>;

It will behave the same as outerIdWithSymbol. The reason is that the type of innerIdWithSymbol is narrowed to a unique symbol, instead of IdentifierWithSymbol<Target>. There are some discussions in #46731.

PS: You can use Bug Workbench to reproduce bug with mutiple files.

@TotooriaHyperion
Copy link
Author

It's intended behavior but needs a better hint.

related discussion
#46731

related suggestion
#45870

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

2 participants