Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Oop 在Typescript中扩展字符串本机类型_Oop_Typescript - Fatal编程技术网

Oop 在Typescript中扩展字符串本机类型

Oop 在Typescript中扩展字符串本机类型,oop,typescript,Oop,Typescript,我想在Typescript中扩展字符串类型。 这是我的密码: export class Str extends String{ constructor(str: string) { super(str); } } 问题是,在使用super(str)调用字符串构造函数之后,我的新类型的值仍然为空 我对扩展本机数组类型的另一个类型也有同样的问题。您的代码被传输到: var __extends = (this && this.__extends) ||

我想在Typescript中扩展字符串类型。 这是我的密码:

export class Str extends String{
    constructor(str: string) {
        super(str);
    }
}
问题是,在使用
super(str)
调用字符串构造函数之后,我的新类型的值仍然为空


我对扩展本机数组类型的另一个类型也有同样的问题。

您的代码被传输到:

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var Str = (function (_super) {
    __extends(Str, _super);
    function Str(str) {
        _super.call(this, str);
    }
    return Str;
})(String);
问题是您不再处理具有特殊属性的原始本机类型(如两个字符串上的
+
运算符)。因此,您的扩展类型(即使您已经成功)将受到严重限制,并且会让其他人感到困惑


有关详细信息和问题,请参阅阿克塞尔·劳施迈耶的文章:

你好,马丁。谢谢你的回答。这是否意味着Typescript无法扩展本机类型?如果是这样的话,唯一的方法就是编写Javascript代码?好吧,它不是用来处理这些特殊情况的内置TypeScript,所以
extends
关键字不能开箱即用。问题是,我认为您无法使用内置类型的属性完全创建自己的字符串。因此,您可以选择修改内置类型,这是一个糟糕的想法。好的,谢谢。事实上,我已经有了解决办法。我只是想重构我的代码,让它更干净。那太糟糕了,我不能用Typescript做这件事。