vendredi 26 mai 2017

Typescript Form Generation

I'm trying to write a compontent that generates a form based on the metadata of a typescript file. I'm having runtime object trouble determining the if a property is of a specific type.

export interface Email{
   value: string;
}

export interface User {
    id: number; 
    firstName: string;
    lastName: string;
    email: Email;
    birthday: Date;
 }

I would like to be able to know the property type /interface type at runtime so I can create the appropriate form elements.

properyLogger<T>(property: T) {
    //This cause a compiler error
    if (property instanceof Email) {
        console.log('Email');
    } else if (property instanceof Date) {
        console.log('Date');
    }
    else if (typeof property === "string") {
        console.log('string');
    } else if (typeof property === "number") {
        console.log('number');
    } 
}

How can I get Interface/Class information at runtime. Alternatively I would be willing to use decorator on the model if that would help. I need this solution to work on most modern browsers





Aucun commentaire:

Enregistrer un commentaire