Attempting to develop a framework that enables meta on interface fields. Consider the following example:
function path(path) {}
interface ITwoProps {
@path('some/path') stringProp: string;
@path('different/path') boolProp: boolean;
}
class Impl1 implements ITwoProps {
get stringProp() {
return 'abc';
}
get boolProp() {
return !!'abc';
}
}
playground link
Unfortunately, this code does not compile due to decorators being disabled on interfaces for non-transpiled code attachment reasons.
Alternative approaches have been considered, such as defining meta and interfaces with schema-like objects:
const Schema = {
boolProp: {
type: Boolean,
path: 'some/path'
},
stringProp: {
type: String,
path: 'different/path'
},
};
type PropertyDef<T> = {
type: (...args: any[]) => T,
path?: string;
};
type ExtractType<X> = X extends PropertyDef<infer T> ? T : never;
type ObjectInterface<T extends {[key: string]: PropertyDef}> = {
[P in keyof T]: ExtractType<T[P]>;
}
class Impl2 implements ObjectInterface<typeof Schema> {
get foo() {
return !!'abc';
}
}
playground link
Issues arise with this approach as well, particularly with PropertyDef
requiring a parameter.
The main question remains - how can an interface be combined with meta in TypeScript? Open to suggestions, even those involving transformers using the Compiler API.