jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

Fix unknown type assignment

Open wojtek1150 opened this issue 3 years ago • 6 comments

Fixing TS2322: Type 'unknown' is not assignable to type 'boolean' in uischema definition

Examples:

  • 278 obj && typeof obj === 'object';
  • 284 obj && typeof obj === 'object';

wojtek1150 avatar Aug 27 '22 08:08 wojtek1150

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Aug 27 '22 08:08 CLAassistant

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.

sdirix avatar Sep 02 '22 14:09 sdirix

Coverage Status

Coverage remained the same at 84.356% when pulling 3c102c668b5a9360371ba9d60f4598121a2289f8 on wojtek1150:patch-1 into 852057ca1706007bee24bcb1b53b88c7231628a0 on eclipsesource:master.

coveralls avatar Sep 02 '22 14:09 coveralls

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

wojtek1150 avatar Sep 02 '22 15:09 wojtek1150

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.

sdirix avatar Sep 05 '22 09:09 sdirix

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.

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)

wojtek1150 avatar Sep 06 '22 10:09 wojtek1150

The reported issue was fixed with https://github.com/eclipsesource/jsonforms/pull/2023

sdirix avatar Oct 04 '22 10:10 sdirix