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 type incorrectly allowed to be used as a value when object shorthand is used #44868

Closed
samouri opened this issue Jul 2, 2021 · 4 comments · Fixed by #44881
Closed
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@samouri
Copy link

samouri commented Jul 2, 2021

Bug Report

Normally, TS will figure out if an imported type is incorrectly being used as a value.
When shorthand object creation is used, TS will miss the error.

⏯ Playground Link

I couldn't figure out how to use the playground since it depends on using multiple files (import/export).
I've created a minimal repro in this repo samouri/typescript-type-import-bug-repro, it is only ~10 LOC.

💻 Code

import type { A } from './a';

// This correctly throws
const object1 = {A: A}

// This should throw the error: "'A' cannot be used as a value because it was imported using 'import type'"
const object2 = { A };

🙁 Actual behavior

No error is thrown

🙂 Expected behavior

An error should be thrown. Types should not be used where values are expected.

@samouri samouri changed the title Import Types incorrectly allowed to be used as a value when object shorthand is used Imported type incorrectly allowed to be used as a value when object shorthand is used Jul 2, 2021
@orta
Copy link
Contributor

orta commented Jul 2, 2021

Strange, this seems to raise 2693 correctly in the TS bug workbench

// @errors: 2693
// @filename: other.ts
export type A = {
    thing: string
}
// @filename: index.ts
import type { A } from './other';

// This should throw the error: "'A' cannot be used as a value because it was imported using 'import type'"
const object = { A }; 

Workbench Repro

@samouri
Copy link
Author

samouri commented Jul 2, 2021

If you change the type to a class then it will reproduce.

// @filename: other.ts
export class A {}

// @filename: index.ts
import type { A } from './other';

// This should throw the error: "'A' cannot be used as a value because it was imported using 'import type'"
const object = { A }; 

Workbench Repro

@samouri
Copy link
Author

samouri commented Jul 7, 2021

Thank you for fixing this so quickly!
Is there any way for me to track which release this makes it into?

@RyanCavanaugh
Copy link
Member

@samouri this will be in TypeScript 4.4 (the RC and final, but not the beta which has already passed).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
6 participants