简体   繁体   English

如何在不键入TypeScript的情况下处理供应商?

[英]How to handle vendors without typings in TypeScript?

I'm thinking about switching from native JS to TS for an application I'm developing. 我正在考虑为我正在开发的应用程序从本机JS切换到TS。 I've created a new branch to test things - Typescript itself seems to be fine for it, but there's one major problem I'm facing: some vendors I'm using do not have any or just outdated definition files. 我创建了一个新分支来测试事物-Typescript本身似乎很合适,但是我面临的一个主要问题是:我正在使用的某些供应商没有任何定义文件,或者只是过时的定义文件。

The question now is: how to handle missing defintion files? 现在的问题是:如何处理丢失的定义文件?

I could do something like 我可以做类似的事情

declare var sails: any;

but is this really a good idea? 但这真的是个好主意吗?

declare var sails: any;

Is a hack. 是骇客。 It's good to get yourself going, or to let you try a library to see if it will actually work, but your best bet is to be explicit. 最好自己动手做,或者让您尝试一个库以查看它是否真正起作用,但是最好的选择是明确。 (Any time you have : any in your project, it should be a red flag that you need to fix something - but realistically almost everyone has an any or two (or more) floating around). (任何时候你: any在你的项目,它应该是你需要修复的东西红旗-但实际上,几乎每个人都有一个any两个(或更多)左右浮动)。 That being said: something as big as sails should not be an any . 话虽这么说:像帆一样大的东西应该是any

So, what can you do? 所以,你可以做什么?

Look on DefinitelyTyped DefinitelyTyped

If you are using Typescript 2, you can install most of the definitions from definitely typed with npm via 如果您使用的是Typescript 2,则可以通过以下方式安装绝大部分使用npm键入的定义:

npm i -g @types/thing-you-want

(unfortunately, it doesn't look like Sails is on DefinitelyTyped) (不幸的是,Sails看起来不像是DefinitelyTyped)

You can also try googling to see if anyone else has written a definition file that's not on definitely typed but that still works. 您还可以尝试使用Google搜索来查看是否有其他人编写了没有明确键入但仍然可以使用的定义文件。

Be prepared though, even definitely typed is not guaranteed to be correct and may need massaging. 但是请做好准备,即使绝对输入也不保证正确,并且可能需要按摩。

Write your definitions as you need them 根据需要编写定义

This may be what you are up against. 这可能是您所要面对的。 This is certainly not as thorough, but it will usually get you where you need to go. 这当然不是那么彻底,但是通常可以带您到达需要去的地方。 Look up the documentation for the portion of the API you need and add those definitions to your .d.ts file. 查找文档中所需的API部分,然后将这些定义添加到.d.ts文件中。

If you go about it this way, it's less overwhelming and you can contribute your work to DefintielyTyped when you are finished if you are so inclined :). 如果您以这种方式进行操作,那么它就不会那么令人头疼,如果您愿意的话,可以在完成后将工作贡献给DefintielyTyped。

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

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