【问题标题】:Typescript: declare empty class object打字稿:声明空类对象
【发布时间】:2018-11-06 12:40:35
【问题描述】:

是否可以从没有默认值的类中声明对象?

我有一个如下所示的类;

export class Month {
 January: string;
 February: string;
 March: string;
 ...
}

mapMyPayload(specialMonths: Birthdays) {
 let myMonth = new Month;
 console.log(myMonth); //=> Month {}
    // here I expect the Object.keys(myMonth) 
    // returns {January=undefined, February=undefined, ...}
    // since I assume the new Month 
    // declares the myMonth as an object with undefined as its value
 Object.keys(myMonth).forEach(m => {
    // iterate thru the keys 
    // check if the month exists inside the specialMonths
    // and is not null or undefined
  if (specialMonths[m] != null) { 
   myMonth[m] = specialMonths[m];
 );
 return myMonth;
}

我想要实现的是检查对象中的任何undefinednull 并返回此类。

我查看了许多示例代码和文档,无论您有隐式构造函数还是显式构造函数,您都可以使用类名前面的 new 来声明新类,但随后它们会声明一些值。所以我认为这些实例在被一些外部范围声明之前是不存在的。

【问题讨论】:

    标签: javascript typescript object


    【解决方案1】:
    class Foo {
      a: string;
      b: string;
      c: number;
    }
    
    var foo = new Foo();
    console.log(foo.a); //undefined
    

    更新: 这会转译为以下 JS 代码:

    var Foo = /** @class */ (function () {
        function Foo() {
        }
        return Foo;
    }());
    var foo = new Foo();
    console.log(foo.a);
    

    您的对象没有任何键,因为它们尚未定义,因此它们使用“未定义”文字而不是“未定义”作为值。 您可以尝试在默认为“未定义”作为值的行中做一些事情:

    class Foo {
      a: string = undefined;
      b: string = undefined;
      c: number = undefined;
    }
    
    var foo = new Foo();
    console.log(foo.a); //undefined
    console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
    Object.keys(foo).forEach(property => console.log(property));
    // keys log as empty and not undefined
    

    这会转译为以下 JS:

    var Foo = /** @class */ (function () {
        function Foo() {
            this.a = undefined;
            this.b = undefined;
            this.c = undefined;
        }
        return Foo;
    }());
    var foo = new Foo();
    console.log(foo.a); //undefined
    console.log(foo); //=> Foo{a: undefined, b: undefined, c: undefined}
    Object.keys(foo).forEach(function (property) { return console.log(property); });
    // keys log as empty and not undefined as part of the forEach loop
    

    我个人建议不要使用这种实现,因为它容易出现意外行为,并且与 typescript 带来的静态类型的 JS 版本背道而驰。

    如果您能更详细地说明您想要实现的目标,或许我们可以为您提供更好的解决方案?

    【讨论】:

    • 我对我的问题进行了更新,我希望这能让您更多地了解我正在努力实现的目标。
    • 如果我的更新答案带来任何快乐,请告诉我。我有一种感觉,您面临的问题在您的代码中有更深的根源,更具体地说是 OOP 原则的错误实现。如果某些特定月份是“特殊的”,但仍具有与常规月份相同的类结构,但添加了“特殊”属性作为布尔值,则可以选择将 Month 类作为 SpecialMonth 类的父类扩展了 Month 类。请参阅以下内容:stackoverflow.com/a/38834997/4183810
    • 感谢 Liviu,感谢您周到的回答,并意识到我怀疑是真的我从某人那里继承了这段代码,所以我试图让它以某种方式工作。
    【解决方案2】:

    为什么不可能?

    类中的字段和属性将被设置为默认值,在你的情况下,如果你没有像这样指定它们的默认值,则字符串为 null:

    export class Month {
       January: string = "Something";
       February: string = "Something";
       March: string = "Something";
       ...
    }
    

    【讨论】:

    • 我也这么认为,但正如您在我的控制台代码中看到的那样,它打印的是 Month {} 而不是 Month {January: undefined, ....}
    • 我查看了许多示例代码以及它在 typescript 文档中的实现方式,它们总是有一个构造函数或一个传递给类的值,就像你需要在定义存在之前填写它一样。我希望这对你有意义。
    • @Kim.LBy 也许这会有所帮助:github.com/Microsoft/TypeScript/issues/8476
    • 感谢@Jovan,只需阅读您的链接,看起来就是我要找的东西。仍然对他们想要达到的目标感到模糊,可能需要第二次才能理解这一点。
    猜你喜欢
    • 2021-01-20
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多