admin管理员组文章数量:1356699
I have the following structure:
project
|- types
|- global.d.ts
|- string.d.ts
|- wdio.d.ts
|- src
|- Models
|- Resources
|- Components
|- Extensions
|- string.ts
|- ...
|- tsconfig.json
|- wdio.conf.js
I try to extend the string's prototype with a function. I tried so far a lot of way, I found on several sites. But either the tsc
gives me error, or the PHPStorm shows error message.
// types/string.d.ts
declare interface String {
myCustomFn(text : string) : string;
}
// src/Extensions/string.ts
String.prototype.myCustomFn = function(text : string) : string {
// ... Logic
return 'myCustomFn';
};
// tsconfig.json
...
"typeRoots": ["./types/"],
"include": [
"./src/**/*.ts",
"./types"
]
...
// wdio.conf.js
...
before: function (capabilities, specs) {
require('ts-node').register({ files: true });
require('../extensions/String');
},
...
I added the augmentation for the String
class to the d.ts file. Then I define the body of the function in a separate file. When I implement it in the src/Extensions/string.ts
file, the tsc
mand gives no error message, BUT the PHPStorm shows the following error:
TS2339: Property 'myCustomFn' does not exist on type 'String'.
Moreover, anywhere in the code the auto-pletition shows my method, and even the code can be executed, and uses the myCustomFn
function.
Questions:
- Is this just an error of the IDE?
- Am I doing something wrong or should the way, how the String class is being extended be in different way?
I have the following structure:
project
|- types
|- global.d.ts
|- string.d.ts
|- wdio.d.ts
|- src
|- Models
|- Resources
|- Components
|- Extensions
|- string.ts
|- ...
|- tsconfig.json
|- wdio.conf.js
I try to extend the string's prototype with a function. I tried so far a lot of way, I found on several sites. But either the tsc
gives me error, or the PHPStorm shows error message.
// types/string.d.ts
declare interface String {
myCustomFn(text : string) : string;
}
// src/Extensions/string.ts
String.prototype.myCustomFn = function(text : string) : string {
// ... Logic
return 'myCustomFn';
};
// tsconfig.json
...
"typeRoots": ["./types/"],
"include": [
"./src/**/*.ts",
"./types"
]
...
// wdio.conf.js
...
before: function (capabilities, specs) {
require('ts-node').register({ files: true });
require('../extensions/String');
},
...
I added the augmentation for the String
class to the d.ts file. Then I define the body of the function in a separate file. When I implement it in the src/Extensions/string.ts
file, the tsc
mand gives no error message, BUT the PHPStorm shows the following error:
TS2339: Property 'myCustomFn' does not exist on type 'String'.
Moreover, anywhere in the code the auto-pletition shows my method, and even the code can be executed, and uses the myCustomFn
function.
Questions:
- Is this just an error of the IDE?
- Am I doing something wrong or should the way, how the String class is being extended be in different way?
1 Answer
Reset to default 8Full working example on Github
Place your String
interface extension in a random .d.ts
file:
interface String {
myCustomFn(text : string) : string;
}
Add another file extension.ts
where you add the prototype
:
String.prototype.myCustomFn = function(text : string) : string {
return 'myCustomFn';
};
Then import the extension.ts
file into your root index.ts
file:
import './extension';
Now you can add your String().myCustomFn(text: string);
everywhere you want.
P.S. it is important that you include the
.d.ts
file to your pilation files. ThetypeRoots
property is not necessary.
tsconfig.json
:
{
"pilerOptions": {
"outDir": "dist"
},
"include": [
"src",
"types" // here is the .d.ts file
],
"exclude": [
"**/node_modules"
]
}
本文标签: javascriptExtending builtin types in TypescriptStack Overflow
版权声明:本文标题:javascript - Extending built-in types in Typescript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744012160a2575765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论