简体   繁体   English

JavaScript库的.d.ts打字稿文件

[英].d.ts typescript file for a javascript library

I'm trying to create a description file to use a javascript library with typescript. 我正在尝试创建一个描述文件以将JavaScript库与Typescript一起使用。

At the moment I'm trying to follow those guides: TypeScript Official website and Medium guide to migrate to typescript 目前,我正在尝试遵循以下指南: TypeScript官方网站迁移至Typescript的中型指南

To be specific I'm trying to create a typescript description file for leaflet-icon-pulse , which I thought was simple enough to start with 具体来说,我正在尝试为leaflet-icon-pulse创建一个打字稿描述文件,我认为这很容易

So I downloaded the git project, created tsconfig.json as follows 所以我下载了git项目,如下创建了tsconfig.json

{
"compilerOptions": {
  "outDir": "./built",
  "allowJs": true,
  "noImplicitAny": true,
  "strictNullChecks": true,
  "target": "es5",
  "module": "commonjs"
},
"include": [
  "./src/**/*"
],
"exclude": [
  "node_modules"
]
}

Then I tried to convert the file L.Icon.Pulse.js to L.Icon.Pulse.ts , but I started getting error like: 然后,我尝试将文件L.Icon.Pulse.js转换为L.Icon.Pulse.ts ,但是我开始收到如下错误:

Property X does not exists on type Y

So as far as I understood I need to let the js be and simply create a L.Icon.Pulse.d.ts file 因此,据我了解,我需要让js保留并简单地创建L.Icon.Pulse.d.ts文件

Right now mine looks like: 现在我的样子:

import * as L from 'leaflet';
declare module 'leaflet' {
     function Pulse(options?: any): any;
}

But now I have no idea how to precede, and I was not able to find a good tutorial or example on how to go on. 但是现在我不知道该怎么做,也无法找到一个很好的教程或示例。

Someone can give me some tips on how to procede? 有人可以给我一些如何进行的提示吗?

Thanks! 谢谢!

Instead of creating the d.ts file, I would maybe go on by ignoring the typescript errors. 与其创建d.ts文件,不如继续忽略打字稿错误。

So, you install leaflet-pulse-icon through npm: 因此,您可以通过npm安装leaflet-pulse-icon:

npm install leaflet-pulse-icon

...and you include it in your component: ...并将其包含在组件中:

import 'leaflet-pulse-icon';

To avoid the error, just make TypeScript ignore the typings for a while.. So, instead of: 为了避免该错误,只需使TypeScript暂时忽略键入即可。

let pulsingIcon = L.icon.pulse({iconSize:[20,20],color:'red'});

You should use: 您应该使用:

let pulsingIcon = (L as any).icon.pulse({iconSize:[20,20],color:'red'});

...using L as "of any type" variable. ...将L用作“任何类型”变量。

Of course, TypeScript should not work without types(!) but occasionally, I find it convenient to bypass some errors using the ":any" way. 当然,TypeScript不能在没有类型(!)的情况下工作,但是偶尔,我发现使用“:any”绕过一些错误很方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM