I'm trying to expose an object literal from a package such that all of the defined properties of the object will be preserved, and viewable when the package is imported and used in some file. For this, I need the shape of the object in the resulting declarations file to not just be the type it is defined as, but the specific properties also.
For example, foo.ts without any typing applied (it is basically it's own type).
const foo = {
bar: 123,
baz: "abc",
};
export default foo;
which compiles to foo.d.ts
declare const foo: {
bar: number;
baz: string;
};
export default foo;
and bar and baz show up as properties of foo when imported and used. All is well.
But now I want to enforce a type on foos properties, to prevent them from being anything.
For example, now I want them all to be numbers (or some other custom type).
So I restrict all of the properties to be only of number type like so in foo.ts.
const foo: { [key: string]: number } = {
bar: 123,
baz: 456,
};
export default foo;
foo.d.ts
declare const foo: {
[key: string]: number;
};
export default foo;
which removes all knowledge of the actual properties of foo, in favour of just showing the type of all properties it may have.
How to achieve the property names being included in the declaration, without leaving the original object untyped?
but there is a problem in what you're doing