简体   繁体   English

从 ts 文件导出多个 typescript 类型别名时出现“意外令牌”错误

[英]Error “ Unexpected token” when export multiple typescript type alias from a ts file

Sandbox: https://codesandbox.io/s/typescript-export-question-tlbd2沙盒: https://codesandbox.io/s/typescript-export-question-tlbd2

error will be on export type {错误将出现在export type {

type abc = {
  a: string;
};

type bbc = {bbb: string}

export type {
  abc,
  bbc,
}

Is exporting types something not suppose to be done?导出类型是不应该做的吗? I am trying to reuse types as much as I can.我正在尝试尽可能多地重用类型。 If there is better alternatives plz let me know如果有更好的选择请告诉我

If you are trying to export the types so that you can import them like this:如果您尝试导出类型以便可以像这样导入它们:

import { abc, bbc } from './my-types';

You can simply export when declaring them:您可以在声明它们时简单地导出:

export type abc = {
  a: string;
};

export type bbc = {bbb: string}

But you might also be looking for TypeScript namespace , if that is the case please look at the documentation here .但您可能也在寻找 TypeScript namespace ,如果是这种情况,请查看此处的文档

I think your only issue is adding the keyword type to your export:我认为您唯一的问题是将关键字type添加到您的导出中:

type abc = {
  a: string;
};

type bbc = {bbb: string}

export { abc, bbc }

Does this work for you or do you still get the same error?这对你有用还是你仍然得到同样的错误?

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

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