Since TypeScript interfaces do not exist at runtime, you cannot use reflection on them. To use reflection I created a class that implements the interface and reflected on the class. However, I was unable to tell if a property is readonly. Not sure if this is a lack of understanding on my part, or a defect. Here is what I tried:
Code
interface ITest {
readonly foo: number;
bar: number;
}
class TestImplementation implements ITest {
readonly foo: number = 1;
bar: number = 2;
}
function reflectOnTest() {
var testImplementation = new TestImplementation();
var properties: string[] = Object.getOwnPropertyNames(testImplementation);
var fooDescriptor = Object.getOwnPropertyDescriptor(testImplementation, properties[0]);
var barDescriptor = Object.getOwnPropertyDescriptor(testImplementation, properties[1]);
console.log("foo writable = " + fooDescriptor.writable);
console.log("bar writable = " + barDescriptor.writable);
}
Output is:
foo writable = true
bar writable = true