-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Resolved type depends on the import order if the aliased type is created by "export { A as B }" #55761
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
Comments
Afaik this is intentionally done for performance reasons. When multiple identifiers refer to the same type, then only the first type will be used. You have the same "issue" with type aliases: Playground link Related: #31940 |
@MartinJohns By checking the difference, I noticed that they are not exactly the same problem. You could write // type.ts
export type FooType = {
value: string;
};
export type AliasFooType = FooType;
export type BarType = {
value: string;
};
export type { BarType as AliasBarType }; then use them in main.ts // main.ts
import { AliasBarType, AliasFooType, BarType, FooType } from './type';
const a: FooType = { value: 'hello' };
const b: AliasFooType = { value: 'hello' };
const c: BarType = { value: 'hello' };
const d: AliasBarType = { value: 'hello' }; The inferred types are (by hovering mouse over the variable):
So there are two ways to make an alias and the second way seems to behave differently from the first way (I don't know the term so...):
If we are using "type alias" the alias type will possibly be resolve to the original type (we can use For Enum, sadly we can only use "export alias" to create a aliased enum. If there're other ways to alias an enum pls let me know. |
export type Bar = Foo;
export const Bar = Foo; |
Ok.. though I don't like the style but at least it works and I don't see any issues with it. |
This issue has been marked as "Not a Defect" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
enum alias export
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about enum
I tested these versions:
β― Playground Link
No response
π» Code
π Actual behavior
The inferred types of
a
andb
in main-1.ts file are:The inferred types of
a
andb
in main-2.ts file are:I made two files main-1.ts and main-2.ts to show that how the import order of enum changes the inferred types.
π Expected behavior
I think there're two accepted bahaviors:
no.1
The inferred types should be:
no.2
The inferred types should be:
So in the code,
Foo
is the original enum andBar
is an alias of it.I think we can accept that typescript infers a var assigned by
Bar.VALUE
to beFoo.VALUE
because it's easy for developers to check the type ofBar
and find that it's an alias ofFoo
. But I don't think it's good for typescript to infer a var assigned byFoo.VALUE
to beBar.VALUE
, because if we assign the original enum to a variable directly, then the inferred type shouldn't be the aliased enum.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: