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

Imported symbol as a type is not merged with a local variable #57414

Closed
Conaclos opened this issue Feb 15, 2024 · 3 comments
Closed

Imported symbol as a type is not merged with a local variable #57414

Conaclos opened this issue Feb 15, 2024 · 3 comments

Comments

@Conaclos
Copy link

Conaclos commented Feb 15, 2024

πŸ”Ž Search Terms

Declaration merging, isolatedModule, type import conflict

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about declaration merging

⏯ Playground Link

https://www.typescriptlang.org/play?isolatedModules=true&ts=5.4.0-beta#code/JYWwDg9gTgLgBDAnmApnA3nAwhcEB2K+8AvnAGZS5wDkUKAhgMYw0DccA9J3ACoDKcFFCpQAznAIIAFsAkB3WQBs0weHLihIsFABM4DCQwTIUAKCYEx8HHkLEAXNlyR78AD5x8AVyVK4ALxevkpsZmZa0PBIqBhwAGJYcGSU1HSMLOxcPAIGfhDyEjCyRhJqNEYmqBZW8IlOiXCePn6BwX5hQA

πŸ’» Code

import type { Component } from 'react'; // TS errors on this while it is imported as a type
const Component: Component | null = null;

import type { FC } from 'react'; // TS allows this as it's a type
const FC: FC | null = null;

πŸ™ Actual behavior

TypeScript reports the following error:

Import declaration conflicts with local declaration of Component.

πŸ™‚ Expected behavior

Because Component is imported as a type, it should be merged with the variable Component without emitting an error.

Additional information about the issue

A related issue was recently fixed and discussed in a design meeting.

Fixing the current issue could make TypeScript behavior more consistent.

@whzx5byb
Copy link

whzx5byb commented Feb 15, 2024

This is intended, since import type {} will also create type information which can be used in typeof if possible. You can write:

// @filename: a.ts
export const Foo = 'Foo';

// @filename: b.ts
import type { Foo } from './a';
type Bar = typeof Foo
  // ^ type Bar = "Foo"

You are not allowed to define a variable Foo here, because it will change the result of typeof Foo.

Workaround: use explicit type definition with import()

// @filename: b.ts
type Foo = typeof import('./a').Foo;
const Foo = 1;
// merged as expected!

@Conaclos
Copy link
Author

Thanks! That's so ugly ;(

@fatcerberus
Copy link

import type {} will also create type information which can be used in typeof if possible.

More specifically, it imports all meanings of the imported symbol, even value meanings, it just doesn't allow using them in emitting positions.

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

3 participants