首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:模块“类型记录-声明”没有导出成员'ICoordinates‘

错误:模块“类型记录-声明”没有导出成员'ICoordinates‘
EN

Stack Overflow用户
提问于 2021-06-26 23:48:04
回答 1查看 3.3K关注 0票数 1

概述:--我正在构建一个TypeScript声明-- NPM/GitHub软件包库(https://github.com/jefelewis/typescript-declarations),希望导入到我的项目中,但导入到项目时遇到了问题。

项目:

代码语言:javascript
复制
import { ICoordinates } from 'typescript-declarations';

错误:

代码语言:javascript
复制
Module '"typescript-declarations"' has no exported member 'ICoordinates'.

我尝试过的:

  • 我试过修改package.json
  • 我试过修改tsonfig.json
  • 我从type导出了src/types/locationTypes.tsinterface

可能的选项:

src/index.ts

代码语言:javascript
复制
// Imports: TypeScript Types
import * as locationTypes from './types/locationTypes';
import * as measurementTypes from './types/measurementTypes';

// Exports
export {
  locationTypes,
  measurementTypes,
};

src/types/locationTypes.ts:

代码语言:javascript
复制
// Module: Location Types
declare module locationTypes {
  // TypeScript Type: Latitude
  export type TLatitude = number;

  // TypeScript Type: TLongitude
  export type TLongitude = number;

  // TypeScript Type: Coordinates
  export interface ICoordinates {
    latitude: TLatitude;
    longitude: TLongitude;
  }
}

// Exports
export default locationTypes;

src/types/measurementTypes.ts:

代码语言:javascript
复制
// Module: Measurement Types
declare module measurementTypes {
    // TypeScript Type: Measurement (Distance)
    export type TMeasurementDistance = 'Kilometers' | 'Miles' | 'Yards' | 'Feet';

    // TypeScript Type: Measurement (Length)
    export type TMeasurementLength = 'Inches' | 'Feet' | 'Yards' | 'Miles' | 'Light Years' | 'Millimeters' | 'Centimeters' | 'Meters' | 'Kilometers';

    // TypeScript Type: Measurement (Speed)
    export type TMeasurementSpeed = 'Centimeters Per Second' | 'Meters Per Second' | 'Feet Per Second' | 'Miles Per Hour' | 'Kilometers Per Hour' | 'Knots' | 'Mach';

    // TypeScript Type: Measurement (Angle)
    export type TMeasurementAngle = 'Degrees' | 'Radians' | 'Milliradians';
}

// Exports
export default measurementTypes;
EN

回答 1

Stack Overflow用户

发布于 2021-06-27 08:58:38

类型定义和包构建管道存在一些问题。

  • 您不必通过cd进入src目录来运行tsc。在您的tsconfig中已经描述了哪些文件应该包含在编译中。
  • 您的cp命令格式错误。cp需要第二个参数。要知道要将源文件复制到的目标文件/目录,请执行以下操作。尽管我相信你不需要在任何地方复制package.json。根目录中的一个很好地描述了您的npm包。不过,如果您有一些我不知道的精心编写的包发布脚本,那么这肯定不是真的。
  • 类型定义中有不必要的内部模块(declare module someModule)。这是一个非常古老的遗留特性,现在用namespaces取代了类型记录。考虑到您是如何导入ICoordinates的,我假设您也不需要名称空间。
  • 出口 locationTypes作为export { locationTypes },您正在创建另一个来自locationTypes.ts类型的名称空间locationTypes

总之,您仍然可以将ICoordinates导入如下:

代码语言:javascript
复制
import { locationTypes } from 'typescript-declarations'

const coords: locationTypes.default.ICoordinates = null as any

不过,如果您希望导入与以下类型相同的类型:

代码语言:javascript
复制
import { ICoordinates } from 'typescript-declarations'

您必须对代码进行一些调整:

代码语言:javascript
复制
// --- src/types/locationsTypes.ts
// TypeScript Type: Latitude
export type TLatitude = number;

// TypeScript Type: TLongitude
export type TLongitude = number;

// TypeScript Type: Coordinates
export interface ICoordinates {
  latitude: TLatitude;
  longitude: TLongitude;
}

// --- src/index.ts
// re-export all members of `locationTypes`
export * from './types/locationsTypes'

如果您想在没有显式命名空间的情况下导入它们,measurementTypes也是如此。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68147021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档