Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a variable in a class to its generic value using Typescript

I am building a component to hold information about fields for use in a form. I need to hold various data objects to create generic routines for working with the data.

export class DataField<T> {
    /**
     * Data field name IE: database column name, JSON field, etc.
     */
    private Name_: string;
    private Size_: number;
    /**
     * The data type for this field. IE: String, Boolean, etc. Note that this is set internally, and is not set by a passed reference
     */
    private Type_: T;
    // private Type_: { new(): T ;}   <- this has not worked either

    /**
     * Class constructor
     * @param FieldName string Name of the Field
     * @param FieldSize number Field Size
     */
    constructor(FieldName:string, FieldSize:number) {
        this.Name_ = FieldName;
        this.Size_ = FieldSize;
    }

    /**
     * Get the data type for the value
     * @returns string The TypeOf the value
     */
    get Type(): string {
        return (typeof this.Type_).toString();
    }
}

The problem that I have is that the field Type_ is not initialized to anything, so it is always set as undefined. When I create the DataField with the generic, it might look like:

new DataField<string>('FullName', 32);

The generic type T is now string, but I want to be able to get the Type_ variable set so the call to get Type() will return a string.

like image 329
Steven Scott Avatar asked Oct 15 '25 16:10

Steven Scott


1 Answers

You'll need to pass the type as a parameter to your constructor; in TypeScript there's no need explicitly set it as a generic:

class DataField<T> {

    constructor( 
        private type: {new (...args): T}, 
        private fieldName: string, 
        private fieldSize: number) {}

    public getType(): string {
        return this.type.name;
    }
}

const field = new DataField(String, 'name', 256);
console.log(field.getType()); // outputs "String"

Remember classes in JS are no more than functions, the class declaration is syntactic sugar. "Types" in JavaScript are no more than the constructor (as in the function object itself) that when called with new will created an object with the desired prototype.

Note you really don't need a generic as your current class is built (I just left it for sake of example); it would function just fine without it. You could replace {new (...args): T} with {new (...args): any}. You would need a generic if you wanted a data container class that returned actual values with the correct type:

class DataContainer<T> {
   // other class stuff

   public getValue(): T {
      // do stuff to return value
   }
}
like image 195
Greg Rozmarynowycz Avatar answered Oct 20 '25 05:10

Greg Rozmarynowycz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!