接口的TypeScript反射

nha*_*123 5 reflection interface typescript

有没有办法找出接口的属性是否定义为只读?说,

interface ITest {
    readonly foo: number;
}
Run Code Online (Sandbox Code Playgroud)

现在,TypeScript是否有某种反思或欺骗手段来获取此信息?例如:

let info = Reflect.get(ITest, 'foo');

if (info.isReadOnly()) { ... }
Run Code Online (Sandbox Code Playgroud)

Wil*_*hti 4

现在有: https: //typescript-rtti.org/

import { reflect } from 'typescript-rtti';

interface ITest {
    readonly foo: number;
}

let iface = reflect<ITest>().as('interface').reflectedInterface;
let info = iface.getProperty('foo').type;

expect(info.isReadonly).to.be.true;
Run Code Online (Sandbox Code Playgroud)

免责声明:我是作者