Skip to content

Basic Usage

Roman Jámbor edited this page Oct 22, 2021 · 2 revisions

Basic Usage

import { getType } from "tst-reflect";

function printTypeProperties<TType>() {
    const type = getType<TType>(); // <--- getting type of generic type in runtime :)
    console.log(type.getProperties().map(prop => prop.name + ": " + prop.type.name).join("\n"));
}

interface SomeType {
    foo: string;
    bar: number;
    baz: Date;
}

class SomeImpl implements SomeType  {
    foo: string;
    bar: number;
    baz: Date;
}

printTypeProperties<SomeType>();

const interfaceType = getType<SomeType>();
const classType = getType<SomeImpl>();

if (classType.isAssignableTo(interfaceType)) { // true
    console.log("Class SomeImpl implements SomeType interface.");
}
Clone this wiki locally