简体   繁体   English

受TypeScript保护和npm软件包

[英]TypeScript protected and npm package

I'm currently building an internal TypeScript "library" with webpack 1.14. 我目前正在使用webpack 1.14构建内部TypeScript“库”。
To do so I created a npm package and published it to a private feed. 为此,我创建了一个npm软件包并将其发布到私有供稿。 Everything works like a charm (I can use classes and interfaces from the library in other "projects" installing the package). 一切都像灵符一样工作(我可以在安装该软件包的其他“项目”中使用库中的类和接口)。

The only thing I'm struggling with is protected members of classes in the library. 我唯一遇到的问题是库中受保护的类的成员。 When a class in a consuming project extends a class from the library it cannot use protected parent members . 当消费项目中的类从库中扩展类时,它不能使用受保护的父成员 The confusing part is that auto completion "sees" the protected members but I get this error during compilation. 令人困惑的部分是,自动完成功能会“看到”受保护的成员,但是在编译过程中会出现此错误。 error TS2339: Property 'apply' does not exist on type 'SitesRollup'

The typescript protected keyword documentation does not state that protected members are restricted to current module, so I'm assuming it should just work. 打字稿受保护的关键字文档没有说明受保护的成员仅限于当前模块,因此我假设它应该可以正常工作。

My question is: should protected members be visible in extending/inheriting classes from different modules? 我的问题是:受保护的成员是否应该在来自不同模块的扩展/继承类中可见?

Finally found where the issue was coming from. 终于找到问题的根源。 The overall type mapping wasn't working properly. 总体类型映射无法正常工作。 This was because of two reasons: 这是由于两个原因:
First I was missing this line in the package.json file "types": "dist/src/index.d.ts", allowing tsc (and my editor) to properly locate the types of the library. 首先,我在package.json文件"types": "dist/src/index.d.ts",中缺少此行"types": "dist/src/index.d.ts",从而允许tsc(和我的编辑器)正确定位库的类型。
Second my library wasn't properly emitting the typings, fixed that adding "declaration": true, to my complierOptions in my tsconfig.json file. 其次我的图书馆没有正确发射分型,固定,添加"declaration": true,complierOptions在我tsconfig.json文件。
Hope that helps. 希望能有所帮助。

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

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