Is there a way to typecheck the value assigned to a variable without widening the type?
For example,
const a = {foo: "hi"} as const; // typeof = {foo: "hi"}
type THasFoo = {foo: string};
const b: THasFoo = {foo: "hi"} as const; // typeof = THasFoo
I'd like to check that whatever I assigned to a has member foo, but without losing the type information of what that value was.
Note: specifically I'm interested in the kind of checking that comes from direct assignment. E.g.
const a = {foo: "hi", bar: "world"} as const;
type THasFoo = {foo: string};
const b: THasFoo = a;
Passes. However,
type THasFoo = {foo: string};
const b: THasFoo = {foo: "hi", bar: "world"} as const;
Does not. It is the latter form of checking I would love to have (but without widening).