在本例中,我在tsconfig.json中使用TS3.9和tsconfig.json一起获得了以下错误:
// a.ts
export const key = 1234
export const obj = {
[key]: 1
};
export default obj;
// b.ts
import A from "./a";
import { key} from "./a"
// Exported variable 'theExport' has or is using name 'key' from external module "c:/tsexample/src/a" but cannot be named.ts(4023)
const theExport = {
A: A,
B: 2,
C: 3,
};
export default theExport
// Exported variable 'theExport' has or is using name 'key' from external module "c:/tsexample/src/a" but cannot be named.ts(4023)
在对相关问题的评论中,TS的PM提出了两个解决方案:
(1)在此情况下不起作用。我试着导出从'a‘到'b’中的所有内容,并且错误消息没有区别。
唯一起作用的是这种非常冗长和难以维护的显式类型注释:
// updated b.ts
import A from "./a";
const theExport: {
// https://github.com/microsoft/TypeScript/issues/9944
[index: string]: typeof A | number;
} = {
A: A,
B: 2,
C: 3,
};
export default theExport;
我的问题是:
这一问题与以下问题相似,但不同:
发布于 2020-10-28 05:29:01
它并不那么漂亮,但这是一个似乎在沙箱中起作用的最小侵入性的改变:
const theExport = {
A: A as {[K in keyof typeof A]: typeof A[K]},
B: 2,
C: 3
};
发布于 2020-10-28 04:30:49
基于这句话,看起来可能有一种解决方案:
// a.ts
export const key = 1234
export const obj = {
[key]: 1
};
export default obj;
// b.ts
import A from "./a";
interface ITheExport {
A: typeof A;
B: number;
C: number;
}
const theExport: ITheExport = { // strong typing the object instead of using inference
A: A,
B: 2,
C: 3,
};
export default theExport
请参阅沙盒
发布于 2020-11-02 11:13:39
显式设置obj类型
// a.ts
export const key = 1234
export const obj = {
[key as number]: 1
}
export default obj
// b.ts
import A from './a'
const theExport = {
A: A,
B: 2,
C: 3
}
export default theExport
https://stackoverflow.com/questions/62538330
复制相似问题