How to resolve "TS2345: Argument of type 'unknown' is not assignable" in calling FileStatNode.is

Hi, I’m currently trying to develop something based off of @theia/navigator (including open-editors-widget ) but for seeing open instances of a locally hosted website accessed within Theia (we’ll call this open-customsite-widget), as talked about in an earlier thread.

Updating my Theia dependencies from 1.26 to 1.28 helped resolve some of the initial issues in migrating open-editors-widget, but one of the issues I’m running into during yarn prepare is that the line from navigator-open-customsite-tree-model.ts (derived from navigator-open-editors-tree-model.ts)…

export namespace OpenCustomsiteNode {
    export function is(node: unknown): node is OpenCustomsiteNode {
        return FileStatNode.is(node) && 'widget' in node;
    }
}

results in error:
error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'object | undefined'. for the call of FileStatNode.is(node) .

But…but…, this is essentially the same code as what’s in navigator-open-editors-tree-model.ts and FileStatNode’s is function supposedly supports the unknown type, as excerpted from file-tree.ts

export namespace FileStatNode {
    export function is(node: unknown): node is FileStatNode {
        return !!node && typeof node === 'object' && 'fileStat' in node;
    }

Any idea what the issue could be?

I can technically workaround this by changing the type to any in OpenCustomsiteNode’s is function, but I’m wondering if there’s a tsconfig setting or something else that might be causing the error to occur.

Hey @vivekgani,

Although the FileStatNode.is function currently accepts unknown, this is a pretty recent change (6 days ago). Before that (i.e. also in 1.28.0) it was object | undefined instead of unknown. You should be able to work around this using the following:

export namespace OpenCustomsiteNode {
    export function is(node: unknown): node is OpenCustomsiteNode {
        return typeof node === 'object' && FileStatNode.is(node) && 'widget' in node;
    }
}
1 Like

Thanks for the pointer! Yep, I was mixing code from the master branch while referencing Theia 1.28. And yes, your solution works after also adding a null check, i.e.

        return (typeof node === 'object' && node != null &&
                FileStatNode.is(node) && 'widget' in node);