I am looking for a way to have an interface automatically get a constant type for some of the fields it has:
type Route = {
path: string;
};
const FooRoute: Route = { path: 'foo' };
FooRoute.path // 'string', but want to have 'foo'
I've been reading about const Type Parameters (https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#const-type-parameters), but it looks like it's only possible for functions. I've also attempted to use readonly:
type Route = {
readonly path: string;
};
const FooRoute: Route = { path: 'foo' };
FooRoute.path // still 'string'
I am aware that I could use satisfies and const:
type Route = {
path: string;
};
const FooRoute = { path: 'foo' as const } satisfies Route;
FooRoute.path // 'foo', but not throught the interface
But this means I have to repeat this quite a lot, is it somehow solvable in the interface itself?