Class Design in TypeScript

I am trying to implement some class in TypeScript, believing that it does not support multiple constructors with different prototypes. Basically, I would like to create classes with an open constructor that takes some parameters, and an "internal" constructor (used only in the library). A public constructor would call internal. In C #, it will look like this:

public class Class1 { internal Class1(int id) { } public Class1(string s) : this(s.Length) { } } 

Any idea how I would translate this to TypeScript?

Thanks in advance!

+4
source share
3 answers

This also works, and perhaps closer to the original:

 class Class1 { id:number; constructor(s: string) { (n:number) => { this.id = n; }(s.length) } } var t:Class1 = new Class1("HELLO"); console.log("Class1ID: " + t.id); // Output = Class1 ID: 5 

For reference, here's the JS output:

 var Class1 = (function () { function Class1(s) { var _this = this; (function (n) { _this.id = n; })(s.length); } return Class1; })(); var t = new Class1("HELLO"); console.log("Class1 ID: " + t.id); 

Update

If you should be able to call the constructor with only an identifier, I think you will have to use the factory method, as Steve suggested. And, since I don't think TS constructors can be private, if you need this method to be private, you have to do without the constructor and use a couple of factory methods. The first instance might look something like this:

 class Class1 { constructor(public id:number) {} // Public, unfortunately. static Fabricate(s:string):Class1 { return new Class1(s.length); } } var classA:Class1 = new Class1(1); var classB:Class1 = Class1.Fabricate("Hello"); console.log(classA.id); // "1" console.log(classB.id); // "5" 

And the second is something like this:

 class Class1 { id:number; private static fabricate(n:number):Class1 { var class1:Class1 = new Class1(); class1.id = n; return class1; } static Fabricate(s:string):Class1 { return fabricate(s.length); } } var classA:Class1 = Class1.Fabricate("Hello"); console.log(classA.id); // "5" 
+4
source

My first thought was to use overloads, but this does not concern your protection of the numerical constructor, it also means adding a condition to the constructor to determine which parameter you passed:

 class MyClass { constructor(s: number); constructor(s: string); constructor(s: any) { } } 

Thus, the pragmatic decision will consist in a particular function:

 class MyClass { constructor(s: string) { this.initialize(s.length); } private initialize(id: number) { } } 
+2
source

With TypeScript 1.4, Union types are now available, allowing you to overload function types. See Description from TypeScript Blog Post . This should allow you to have several types of constructor input with one design function.

Connection types

JavaScript functions can take several possible types of arguments. So far, we have supported this with function overloads. Starting with TypeScript 1.4, we have summarized this feature and can now indicate that a value is one of several different types using a union type:

 function f(x: number | number[]) { if (typeof x === "number") { return x + 10; } else { // return sum of numbers } } 

Once you have a union type value, you can use type checks and instanceof to use this value in a safe way. You will notice that we use this in the above example and can consider x as a numeric type inside an if block.

Union types are a new type of type and work anywhere you specify a type.

0
source

All Articles