无法在 svelte 中使用带有打字稿的已安装类型

时间:2021-02-15 16:43:59

标签: typescript svelte

我很难让我的项目使用已安装的类型进行编译。我正在使用 MediaRecorder,并且我已经安装了 @types/dom-mediacapture-record。在默认设置下,我无法识别 MediaRecorder。

我设法通过 1) 不扩展 @tsconfig/svelte/tsconfig.json 和 2) 在类型下添加特定类型和“svelte”:“types”: ["sevlte", "@类型/dom-mediacapture-record"]。我无法使用 elseifs

使其正常工作

我在网上找不到其他人遇到这种问题的例子,所以我觉得我一定遗漏了一些明显的东西。

1 个答案:

答案 0 :(得分:0)

以下是我快速尝试并似乎有效的方法(不需要对 tsconfig.json 进行任何更改):

编辑

除了 node_modules 之外,我发现在从当前项目导入某些类型(而不是从 dom-mediacapture-record 导入的某些类型)时,我的原始代码存在问题。将 interface Window... 包裹在 declare global {...} 中可以解决这个问题。所以这里是更新的代码:

// add a file "<your filename here>.d.ts" to your src directory containing 
// the following code:

// import the types from @types/dom-mediacapture-record in order
// to extend the existing Window type with them
import * as dmr from "dom-mediacapture-record";
import type { MyCustomInterface } from "./customStuff";

// Using TypeScript's declaration merging, "extend" the existing 
// Window interface
declare global {
   interface Window extends dmr, MyCustomInterface {}
}

编辑前的版本:

// add a file "<your filename here>.d.ts" to your src directory containing 
// the following code:

// import the types from @types/dom-mediacapture-record in order
// to extend the existing Window type with them
import * as dmr from "dom-mediacapture-record";

// Using TypeScript's declaration merging, "extend" the existing 
// Window interface
interface Window extends dmr {}