We encountered a compiling error after upgrading from TypeScript 4.7.4 to a newer version (specifically, 4.8.4). This error was not present in our codebase when using 4.7.4.
To pinpoint the issue, I have extracted the error into a small code snippet. When compiling with 4.8.4 or any subsequent versions, we receive an error message similar to this:
src/example2.ts:19:62 - error TS2345: Argument of type 'object' is not assignable to parameter of type 'Record<string, BasicValue>'.
Index signature for type 'string' is missing in type '{}'.
19 typeof value === 'object' && areFieldsValid(PFields, value, isValidPField);
~~~~~
This issue seems to align with a potential breaking change outlined in the TypeScript 4.8 release notes available at
The sample code causing this error includes:
export interface PNode {
'text'?: string
}
export const PFields = ['text'];
export type DefinedBasicValue = number | boolean | string | Array<BasicValue> | {} | {
[key: string]: BasicValue
[key: number]: BasicValue
}
export type BasicValue = undefined | DefinedBasicValue
/**
* Take any object, value, undefined, or null, and determine if it is a PNode
*/
export const isPNode = (value?: {}): value is PNode =>
typeof value === 'object' && areFieldsValid(PFields, value, isValidPField)
export function areFieldsValid(fields: string[], value: Record<string, BasicValue>, ...validations: ((field: string, value: BasicValue) => boolean)[]): boolean {
return true
}
export const isValidPField = (field: string, value: BasicValue): boolean => true
The root of the problem lies in the value
argument used in the function call on line 19:
areFieldsValid(PFields, value, isValidPField)
.
We seek assistance in understanding why this behavior exists in recent TypeScript versions but not in older ones. Moreover, what would be the most appropriate way to address such errors without resorting to workarounds?
Playground with v4.7.4 demonstrating no error occurrences
Playground illustrating v4.8.4 error manifestation
Playground showcasing current version (v5.4.5) indicating the persisting error