You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

导入typescript-declarations库时提示无导出成员ICoordinates如何解决

问题根因

你当前的导出结构和导入写法不匹配,同时存在不必要的模块嵌套:

  • 你的src/index.ts只导出了locationTypes、measurementTypes两个顶层对象,ICoordinates是嵌套在locationTypes下的属性,直接从包根路径导入自然找不到对应成员
  • 类型文件中额外套了一层declare module,属于冗余写法,反而增加了导出层级
修复方案

步骤1:简化类型文件写法,删除多余的declare module包装

修改src/types/locationTypes.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/types/measurementTypes.ts,直接导出各个类型即可,不需要外层的declare module和默认导出:

// 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';

步骤2:调整入口导出逻辑,按需选择导出方式

方案A:平级导出所有类型(推荐,符合你当前的导入写法需求)

修改src/index.ts为以下内容,将所有类型直接提升到包的根导出:

export * from './types/locationTypes';
export * from './types/measurementTypes';

修改后即可直接用你原本的写法导入:

import { ICoordinates } from 'typescript-declarations';

方案B:保留命名空间分类导出

如果需要按类型分类保留命名空间,修改src/index.ts为:

export * as locationTypes from './types/locationTypes';
export * as measurementTypes from './types/measurementTypes';

对应导入写法需要调整为:

import { locationTypes } from 'typescript-declarations';
type ICoordinates = locationTypes.ICoordinates;

步骤3:确认打包配置正确

  • 确保tsconfig.json中开启了"declaration": true,会自动生成类型声明文件
  • 确保package.json中types字段指向正确的类型入口(比如"types": "./dist/index.d.ts"),和你打包后的产物路径匹配
疑问解答

不需要在index.ts中使用/// <reference />引用模块,该语法是旧版TS用于处理全局命名空间、非ES模块类型的语法,你当前使用标准ES模块导出导入体系,无需用到该写法。

内容的提问来源于stack exchange,提问作者jefelewis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.14 06:12:02