In Figma plugin development with TypeScript, the global figma
variable works exactly the same as in JavaScript - you simply write figma
and access its properties and methods directly[1][3].
The figma
global object is available throughout your main plugin code and can be used identically to JavaScript:
// Works the same in TypeScript as JavaScript
const localCollections = await figma.variables.getLocalVariableCollectionsAsync();
const selection = figma.currentPage.selection;
const component = figma.createComponent();
The main difference with TypeScript is that you get type safety and autocompletion. Figma provides official TypeScript typings that give you:
- Type annotations for all API methods and properties
- IntelliSense/autocompletion in your editor
- Compile-time error checking
For example, when accessing selection, TypeScript knows the correct type:
const selection: SceneNode[] = figma.currentPage.selection;
One crucial point to remember is that the figma
global object is only available in your main plugin code (typically code.ts
), not in UI files (ui.tsx
)[4]. If you need to access Figma's API from your UI, you must send messages from the UI to the plugin code[4].
To get the latest TypeScript typings and full IDE support, install the official typings package[3]:
npm install --save-dev @figma/plugin-typings
This provides comprehensive type definitions for the entire Plugin API, helping you catch errors and understand the available methods and properties as you develop your plugin[3].
[1] https://www.figma.com/plugin-docs/working-with-variables/ [2] https://www.figma.com/plugin-docs/typescript/ [3] https://www.figma.com/plugin-docs/api/api-reference/ [4] https://forum.figma.com/ask-the-community-7/how-i-can-get-access-to-global-object-figma-in-ui-tsx-26779 [5] https://9elements.com/blog/variables2css-writing-a-figma-plugin/ [6] https://www.figma.com/plugin-docs/api/data-types/ [7] https://www.growthaccelerationpartners.com/blog/enhancing-design-efficiency-creating-a-custom-figma-plugin [8] https://help.figma.com/hc/en-us/articles/15339657135383-Guide-to-variables-in-Figma [9] https://www.youtube.com/watch?v=pA4n712h1Vc [10] https://baselinedesign.io/posts/baseline-14-a-guide-to-variables-in-figma
The
figma
global variable is accessible in TypeScript exactly the same way as JavaScript - you simply writefigma
directly in your code.In Main Plugin Code (code.ts)
The
figma
global object is available throughout your main plugin TypeScript file:Important Limitation: UI Files
The
figma
global object is NOT available in UI files (likeui.tsx
)[1]. If you need to access Figma's API from your UI code, you must send messages from the UI to the plugin code:In UI code (ui.tsx):
In plugin code (code.ts):
TypeScript Benefits
With TypeScript, you get enhanced development experience when accessing the
figma
global:Getting TypeScript Support
To get full TypeScript support, install the official typings:
This provides complete type definitions for the entire Plugin API, giving you proper IntelliSense and type checking when working with the
figma
global object[5].[1] https://forum.figma.com/ask-the-community-7/how-i-can-get-access-to-global-object-figma-in-ui-tsx-26779
[2] https://www.figma.com/plugin-docs/typescript/
[3] https://www.tonyward.dev/articles/figma-variables-to-css-variables
[4] https://www.figma.com/plugin-docs/working-with-variables/
[5] https://github.com/craftzing/fft-example-figma-variables
[6] https://help.figma.com/hc/en-us/articles/15339657135383-Guide-to-variables-in-Figma
[7] https://github.com/Supernova-Studio/figma-typescript-api
[8] https://www.youtube.com/watch?v=1ONxxlJnvdM
[9] https://dev.to/onyeneke/how-to-use-variables-in-figma-2599
[10] https://www.youtube.com/watch?v=pA4n712h1Vc