jsonforms
jsonforms copied to clipboard
Fix unknown type assignment
Fixing TS2322: Type 'unknown' is not assignable to type 'boolean' in uischema definition
Examples:
- 278 obj && typeof obj === 'object';
- 284 obj && typeof obj === 'object';
Hi @wojtek1150, how did you run into this problem? When I use the regular development setup I don't get this Typescript error reported.
Either way I don't think the fix is to change unknown to any. unknown is used so Typescript helps us writing valid type guards for any kind of value which could be passed to the type guard. By using any Typescript will not help us at all and we might miss some checks.
Would !!obj also work for you? This should convert the unknown into a valid boolean.
Coverage remained the same at 84.356% when pulling 3c102c668b5a9360371ba9d60f4598121a2289f8 on wojtek1150:patch-1 into 852057ca1706007bee24bcb1b53b88c7231628a0 on eclipsesource:master.
This is a fix. Since property can be anything. In the function you are checking type of the input. It cannot be boolean since it is unknown. From typescript documentation:
Anything is assignable to unknown, but unknown isn't assignable to anything but itself
You cannot reproduce because you have disabled multiple ts checks
Shouldn't !!obj convert the unknown to a boolean and therefore fix the error? I would rather not change unknown to any as this just bypasses the Typescript checks.
Shouldn't
!!objconvert theunknownto abooleanand therefore fix the error? I would rather not changeunknowntoanyas this just bypasses the Typescript checks.
as unknown as boolean will also bypass typescript check, see example: https://stackoverflow.com/a/72078863/2782133
But !!obj seems to be right choice so we can get right of typeof null // 'object' issue. I'll use that way.
Anyway I'm not in the project so I can ommit something, but IMO here it could be good place for overloading with all possible (from code not type :P) scenarios so you will have 100% sure what type can be here (in the future ofc)
The reported issue was fixed with https://github.com/eclipsesource/jsonforms/pull/2023