π Search Terms
"object record"
"object index"
"record index"
"property access"
"object unknown"
"guard object"
β
Viability Checklist
β Suggestion
Explanation of the solving problem
When working with unknown, a very common runtime pattern to ensure an object-like structure is:
obj !== null && typeof obj === 'object'
However, TypeScript currently narrows this to object, which is not indexable and then prevents any property access.
(I checked with noUncheckedIndexedAccess enabled)
Actual workaround
The workaround that actually working for now:
const isRecord = (obj: unknown): obj is Record<string,unknown> => {
return obj !== null && typeof obj === 'object'
}
const processUnknownObjectWithAssertion = (obj: unknown) => {
if (!isRecord(obj)) return;
obj.test; // Works because obj is Record<string,unknown>, then obj.test is unknown
}
Feature request
This leads to extra type check when it's sometime not expected. Would be nice to add an options to type obj !== null && typeof obj === 'object' as Record<string,unknown>.
Resource
Feel free to test the see the snippet here.
π Motivating Example
Current behavior:
const processUnknownObject = (obj: unknown) => {
if (obj === null) return;
if (typeof obj !== 'object') return;
obj.test; β Error: Property 'test' does not exist on type 'object'
}
Expected behavior:
const processUnknownObject = (obj: unknown) => {
if (obj === null) return;
if (typeof obj !== 'object') return;
obj.test; // Works because obj is Record<string,unknown>, then obj.test is unknown
}
π» Use Cases
-
What do you want to use this for?
Make the use of TS easier and more configurable.
-
What shortcomings exist with current approaches?
Add an option in the tsconfig to guard obj !== null && typeof obj === 'object' as Record<string,unknown>.
-
What workarounds are you using in the meantime?
Manually use a type assertion function:
const isRecord = (obj: unknown): obj is Record<string,unknown> => {
return obj !== null && typeof obj === 'object'
}
π Search Terms
"object record"
"object index"
"record index"
"property access"
"object unknown"
"guard object"
β Viability Checklist
β Suggestion
Explanation of the solving problem
When working with unknown, a very common runtime pattern to ensure an object-like structure is:
However, TypeScript currently narrows this to object, which is not indexable and then prevents any property access.
(I checked with noUncheckedIndexedAccess enabled)
Actual workaround
The workaround that actually working for now:
Feature request
This leads to extra type check when it's sometime not expected. Would be nice to add an options to type
obj !== null && typeof obj === 'object'asRecord<string,unknown>.Resource
Feel free to test the see the snippet here.
π Motivating Example
Current behavior:
Expected behavior:
π» Use Cases
What do you want to use this for?
Make the use of TS easier and more configurable.
What shortcomings exist with current approaches?
Add an option in the tsconfig to guard
obj !== null && typeof obj === 'object'asRecord<string,unknown>.What workarounds are you using in the meantime?
Manually use a type assertion function: