Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Add basic type guards to code utils #16

Open
Sidnioulz opened this issue Sep 12, 2024 · 0 comments
Open

Suggestion: Add basic type guards to code utils #16

Sidnioulz opened this issue Sep 12, 2024 · 0 comments

Comments

@Sidnioulz
Copy link

Hi folks,

Loving your plugin, kudos for releasing it as FOSS! It's such a time saver to have a basic bundler and UI framework integration from the get go!

I'd like to suggest adding type guard functions for common node operations as part of the template, e.g. hasChildren or isComponentNode, to helps folks less familiar with TypeScript not get stuck on a common issue with handling Figma Plugin API data.

// src-code/utils/type-guards.ts
export function isComponentNode(node: BaseNode): node is ComponentNode {
  return node.type === 'COMPONENT';
}

// src-code/code.ts
listenTS("processComponent", async (res) => {
  const node = await figma.getNodeByIdAsync(res.nodeId);

  if (!isComponentNode(node)) {
    throw new Error(`Node ${nodeId} is of type '${node.type}', expected 'COMPONENT'.`)
  }

  // Do things that require a component node here...

  dispatchTS("processComponentCallback", { result: true });
});

I think this is something that could easily grow over time, and would also be easy to adjust and extend, but folks not familiar with type guards would likely greatly benefit from an example.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant