gpt4 book ai didi

TypeScript 定义文件找不到 .d.ts

转载 作者:行者123 更新时间:2023-12-05 05:17:00 25 4
gpt4 key购买 nike

上下文:我正在尝试为 library 创建一个定义文件我不是作者。使用我创建的定义文件时,TypeScript 提示找不到定义文件。

我已经尝试了几种方法,这里是最后三种:

首先尝试(inspired by a similar library d.ts):

declare class MessageFormat {
constructor(message: string);

compile: (messageSource: string) => Msg;

}
export type Msg = (params: {}) => string;
export default MessageFormat;

Could not find a declaration file for module 'messageformat'. 'node_modules/messageformat/lib/messageformat.js' implicitly has an 'any' type.

第二次尝试(from TypeScript's example to write d.ts)

declare class MessageFormat {
constructor(message: string);

compile: (messageSource: string) => MessageFormat.Msg;

}
declare namespace MessageFormat {
type Msg = (params: {}) => string;
}
export = MessageFormat;

Could not find a declaration file for module 'messageformat'. 'node_modules/messageformat/lib/messageformat.js' implicitly has an 'any' type.

第三次尝试(from a GitHub question)

declare module "messageformat" {
export type Msg = (params: {}) => string;
export interface MessageFormat {
new(message: string): any;
compile: (messageSource: string) => Msg;
}
}

Cannot use 'new' with an expression whose type lacks a call or construct signature.

代码: 三个暂定版本在这个 repo 中:https://github.com/MrDesjardins/importdefinitionfiles

谁能指出我做错了什么?

最佳答案

这对我有用,在你项目的一些 d.ts 文件中,文件名是不相关的。 DO 确保“messageformat”是节点模块的实际名称,否则您的打包程序将失败。

declare module "messageformat" {
type Msg = (params: {}) => string;
class MessageFormat {
constructor(locale: string | string[] | Object);
compile(messageSource: string): Msg;
}
export = MessageFormat;
}

现在,在其他一些模块中:

import MessageFormat from "messageformat";
const mf = new MessageFormat("en");
const thing = mf.compile("blarb");

关于TypeScript 定义文件找不到 .d.ts,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49697563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com