In simplest terms, this is TypeScript warning you about the fact that a specific property you are trying to access may not be present on the nodes depending on the type.
It’s a TypeScript error, which doesn’t mean your code is wrong, it means TypeScript thinks it’s wrong. It knows that findAll
function and other similar functions can return all types of nodes (SceneNode
), and it doesn’t know that your condition will only return objects of a specific type.
There are four ways to “fix” this:
-
Use a simple condition to check if the node is the type you need:
if (node.type === 'FRAME') { /* do something with a frame */ }
-
But what if you only want to check a specific property and don’t care about node type? Use
if ('children' in node) { /* do something with a node that has children */}
— see documentation here. -
The outdated proper way (but still useful in some cases) is be to implement a function with the type predicate and make sure your object is indeed a
FrameNode
(or whatever other types you need). Example function:// Returns type predicate and true if node supports children function isFrameOrGroup(node: SceneNode): node is FrameNode | GroupNode { return node.type === 'FRAME' || type === 'GROUP' }
Usage:
if ( isFrameOrGroup(node) ) { /* do something */ }
This was necessary before because
node.type
had no relation to the TypeScript types. Now TypeScript got smarter so this is not required if the method above is sufficient for your needs. -
If you know the type of objects you will be getting explicitly (and you are sure they won’t be different), you can make a type assertion:
node as FrameNode
.Both of these things are explained in the TypeScript docs: https://www.typescriptlang.org/docs/handbook/advanced-types.html
-
For testing purposes, you can simply use
// @ts-ignore
comment above each line which you want TypeScript to ignore completely.
Alternatively, you can switch entirely to plain JavaScript if you don’t want to deal with typing issues.