TypeScript 默认类型在使用时被导入,但未标记为从未找到的模块中导入,

7lrncoxx  于 2个月前  发布在  TypeScript
关注(0)|答案(3)|浏览(34)

🔎 搜索词

不能用作值,因为它是通过 import type 导入的

🕗 版本与回归信息

  • 我尝试的每个版本都有这种行为

⏯ Playground 链接

https://www.typescriptlang.org/play?ts=5.4.2#code/JYWwDg9gTgLgBDAnmApnAKnAZlCIBQA9IXAAIwDOAtCgB6oDGMNUuU+ccA5CBACZcA3PnwMIAOwrx4AXgzD8oSLDgBvJKjgBVAL7ZcBYmUo16KJizYduvAQrGT4AVzhytwoA

💻 代码

import type T from
// @ts-expect-error
  'mod';

const t = T;

import {type U} from
// @ts-expect-error
  'mod';

const u = U;

🙁 实际行为

TS 在 T 的使用上没有报错。
TS 在 U 的使用上报错:'U' 不能作为值使用,因为它是通过 'import type' 导入的。(1361)

🙂 预期行为

TS 在 T 的使用上报错:'T' 不能作为值使用,因为它是通过 'import type' 导入的。(1361)
TS 在 U 的使用上报错:'U' 不能作为值使用,因为它是通过 'import type' 导入的。(1361)

关于问题的附加信息

  • 没有回复*
wvmv3b1j

wvmv3b1j1#

对于我来说效果很好。我认为你的问题是由$x_1^m_n^x$引起的吗?我建议使用bug workbench而不是playground,因为前者可以处理多个文件之间的导入/导出。

https://www.typescriptlang.org/dev/bug-workbench/

代码

// 示例代码
function add(a: number, b: number): number {
  return a + b;
}

const result = add(1, 2);
console.log(result); // 输出:3
4sup72z8

4sup72z82#

think your issue is caused by @ts-expect-error?
It is not - the comment specifically only applies to the import source - hence the weird placement.
Also note in your bug workbench the placement of the error - it is on the const t = T; , not on the import declaration.
Also TIL about the bug workbench. I hadn't seen that before.
It looks like this specifically occurs then if the imported module does not exist.

0kjbasz6

0kjbasz63#

看起来这个特定情况发生在导入的模块不存在时。
那么这有关系吗?要清楚的是,这仍然是一个bug,因为你不能在值位置使用类型导入,但在实践中,你已经在导入时得到一个错误,而且我认为人们通常不会去解决模块解析错误。

相关问题