简体   繁体   English

在 TypeScript 中使用动态导入的类型

[英]Type with dynamically imported type in TypeScript

I'm junior frontend developer我是初级前端开发人员

Currently, working on multilingual project with next.js + typescript目前,正在使用 next.js + typescript 开发多语言项目

I'm trying to import type from each locale dynamically (kr, en)我正在尝试从每个语言环境动态导入类型(kr,en)

Is it possible to type with dynamically imported type in TypeScript..?是否可以在 TypeScript 中使用动态导入的类型进行键入..?

So here's what I've tried所以这就是我尝试过的

// @project/kr/foo
export enum foo {
 KIMCHI = 'kimchi',
 JOKBAL = 'jokbal'
}

// @project/en/foo
export enum foo {
 CHEESE = 'cheese',
 PIZZA = 'pizza'
}

// locale common component 
const { foo } = require(`@project/${process.env.LOCALE}/foo`)

interface bar {
 foo: typeof foo
}

With this, I get 'any' type for foo有了这个,我得到 foo 的“任何”类型

If you know and tell me possible way to enable this, You'll be my savior..如果您知道并告诉我启用此功能的可能方法,您将成为我的救星..

Thank you :)谢谢 :)

要在 typescript (.ts) 文件中导入:

import { foo } from "@project/en/foo";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM