π Search Terms
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
β
Viability Checklist
β Suggestion
I'd like to be able to check if a property is in an object and then access that property without needing a type cast.
π Motivating Example
given something like:
declare const props: Partial<Record<string, string>>;
I want to be able to access properties in it using in as a type guard:
const answer: string = "answer" in props ? props.answer : "42";
my current workaround is to cast:
const answer: string = "answer" in props ? props.answer as string : "42";
π» Use Cases
- What do you want to use this for? type guards/narrowing
- What shortcomings exist with current approaches? I have to cast even though I think TypeScript should already be able to detect that the cast is unnecessary
- What workarounds are you using in the meantime? manually cast
π Search Terms
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/in
β Viability Checklist
β Suggestion
I'd like to be able to check if a property is
inan object and then access that property without needing a type cast.π Motivating Example
given something like:
I want to be able to access properties in it using
inas a type guard:my current workaround is to cast:
π» Use Cases