I have read multiple posts and issues on here with respect to narrowing an object of type unknown in TypeScript. But I have not found this specific question or solution.
Most solutions I find state to do something like this.
const result: unknown = {
movieName: "test",
};
if (
typeof result === "object" &&
result !== null &&
"movieName" in result &&
typeof result.movieName === "string" // <-- Error Here
) {}
The error states
Property 'movieName' does not exist on type 'object'
How do I narrow it so that it knows that the unknown thing is an object which contains the property movieName? How do I access result.movieName for an unknown type?
edit: perhaps it's not possible? and the recommendation in the comments below of declaring it as a Record<string, unknown> may be the only way per this GitHub request?