如何在index.d.ts打字稿定义文件中引用Redux类型?

Way*_*oss 3 javascript intellisense typescript visual-studio-code typescript-typings

我想创建一个index.d.ts文件,该文件引用来自称为的流行NPM包中的某些类型redux

我已经尝试过的事情:

(在我index.d.ts文件的顶部)

  1. /// <reference path="../../../node_modules/redux/index.d.ts" />

这行不通。我怀疑这是因为redux/index.d.ts文件根本没有声明名称空间。当我编辑redux/index.d.ts文件并用包裹所有内容时declare namespace Redux { ... },一切正常。我在redux@^4.0.0

  1. /// <reference types="redux" />

除非我安装了NPM软件包@types/redux/@3.6.31,否则它将无法工作,该软件包的类型过时且与发布的类型不匹配。最新版本的@types/redux软件包只是一个已弃用的占位符,该占位符表示要使用正式的redux软件包类型(不起作用)。

  1. import { Middleware } from "redux";

当我将import上面显示的语句放入index.d.ts文件中时,文件中的每个声明都中断了(即,我依赖于自己的index.d.ts文件进行类型定义的每个其他位置都停止从中读取任何类型。)文件import类型的语句d.ts使Typescript将该文件视为模块,而不是定义文件。

无效的(IMO)解决方法虽然可行,但实际上并非如此:

  1. 正在安装NPM软件包,@types/redux@3.6.31而不是最新的不推荐使用的占位符版本@types/redux
  2. 编辑我的副本node_modules/redux/index.d.ts以将所有内容包装在名称空间中,例如declare namespace Redux { ... }

其他信息:

我的项目使用create-react-appv2作为JavaScript项目进行了引导。我只是使用d.ts文件来记录一些接口。我的编辑器是VSCode最新/稳定的。

Ale*_*rck 6

Since typescript 2.9 (see section on import types) you can use import() to use the type definition files without actually importing the module itself. For example this definition will work using the Action type from the redux package:

declare function createCustomAction(): import("redux").Action
Run Code Online (Sandbox Code Playgroud)

No need to reference anything at the top with the triple slash directive.