无法在 svelte 中使用带有打字稿的已安装类型
unable to use installed types with typescript in svelte
我很难让我的项目使用已安装的类型进行编译。我正在使用 MediaRecorder,我已经安装了@types/dom-mediacapture-record。使用默认设置,我无法识别 MediaRecorder。
我已经设法通过 1) 不扩展 @tsconfig/svelte/tsconfig.json 和 2) 在类型下添加特定类型和“苗条”:“类型”:[ “sevlte”,“@types/dom-mediacapture-record”]。我无法使用 typeRoots
使其正常工作
我在网上找不到任何其他人遇到此问题的示例,所以我觉得我一定遗漏了一些明显的东西。
以下是我快速尝试并似乎有效的方法(不需要对 tsconfig.json
进行任何更改):
编辑
除了 dom-mediacapture-record
之外,我还发现从当前项目导入某些类型(而不是从 node_modules
导入某些类型)时我的原始代码存在问题。在 declare global {...}
中包装 interface Window...
解决了这个问题。所以这是更新的代码:
// 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 {}
我很难让我的项目使用已安装的类型进行编译。我正在使用 MediaRecorder,我已经安装了@types/dom-mediacapture-record。使用默认设置,我无法识别 MediaRecorder。
我已经设法通过 1) 不扩展 @tsconfig/svelte/tsconfig.json 和 2) 在类型下添加特定类型和“苗条”:“类型”:[ “sevlte”,“@types/dom-mediacapture-record”]。我无法使用 typeRoots
我在网上找不到任何其他人遇到此问题的示例,所以我觉得我一定遗漏了一些明显的东西。
以下是我快速尝试并似乎有效的方法(不需要对 tsconfig.json
进行任何更改):
编辑
除了 dom-mediacapture-record
之外,我还发现从当前项目导入某些类型(而不是从 node_modules
导入某些类型)时我的原始代码存在问题。在 declare global {...}
中包装 interface Window...
解决了这个问题。所以这是更新的代码:
// 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 {}