问题描述:

For some time I am wondering if we should use .d.ts files in pure TypeScript project or not?

If not, is there any convention of file name that keeps interfaces only in case, that main file grows to large already, and we wish to clean it up by splitting interfaces and actual implementations?

What is nice in .d.ts is that you can't write actual executable code there,

so you cannot by accident load useless file in browser, but I have a feeling that this is misusage of definition files, and that they should be used only in combination with JS code.

If so, where to place interfaces then? "types.ts" or "interfaces.ts"?

网友答案:

I think you are mixing two things here: interfaces (as a concept in TypeScript) and d.ts files. As I see it they are serving two different purposes:

  • interface - used to provide structure to your types that are commonly used throughout your program (link). How to split your program into interfaces is too broad and subjective topic to be answered here.
  • d.ts files - are used to provided type information for the projects that are missing one.

Having said that - I think you should not use d.ts files as part of your typescript program development - they will actually be the result of its transpiling into javascript.

For example if you are developing npm module in typescript, you are going to publish it transpiled into JS + d.ts files. Because you want end user to be able to import the module without transpilation it again + have all type information available.

相关阅读:
Top