The @forcetrails/sobject-types Tool is a command-line utility built to automate the generation of TypeScript definition files (.ts) for Salesforce objects. These definition files are crucial for projects containing TypeScript LWC components or LWC components with JSDocs, providing type safety and autocompletion support.
The tool fetches metadata for specified Salesforce objects, extracts fields and their corresponding data types, and generates TypeScript interfaces that mirror the structure of these Salesforce objects.
Before installing and using the tool, ensure you have the following prerequisites:
- Node.js (version >= 12.0.0)
- npm (Node Package Manager)
- Salesforce CLI installed and authenticated with at least one Salesforce organization. Install Salesforce CLI from here.
npm i @forcetrails/sobject-typesUpdate the ./ftypes/ftypesc.json file with your Salesforce credentials and desired configuration (e.g., Salesforce objects to generate .ts files for, output directory).
npx ftypes- Update the
tsconfig.jsonto include the output folder i.e../ftypesby default to add paths for@sobject-types.
{
"compilerOptions": {
// ...
"paths": {
"@sobject-types/*": ["./.ftypes/typings/*"]
}
}
// ... other config.
}// 1. -----> import type
import { Account } from '@sobject-types/Index';
import { LightningElement, api } from 'lwc';
export default class TodoItem extends LightningElement {
// 2. -----> use type
account: Account = {
ChildAccounts: [],
CleanStatus: 'NotFound',
Type: 'Channel Partner / Reseller',
Industry: '',
Ownership: 'Other',
Rating: 'Cold',
};
}// 1. -----> Import the type
/**
* @typedef {import('./.ftypes/typings/Index.ts').Account} Account
*/
import { LightningElement, api } from 'lwc';
export default class TodoItem extends LightningElement {
// 2. -----> Use the Type
/**
* @type Account
*/
account = {
ChildAccounts: [],
CleanStatus: 'NotFound',
Type: 'Channel Partner / Reseller',
Industry: '',
Ownership: 'Other',
Rating: 'Cold',
};
}ftypes initInitialize the ftypes configurationftypes listList all sObjects from config fileftypes configShow config contentsftypes set-target-org <orgname>Set the default Salesforce organization username in./ftypes/ftypesc.json. **This is alias of already authenticated Salesforce org from sf authftypes add <objname>Add type definitions for a specific objectftypes refresh <objname...>Add type definitions for a specific object(s)ftypesGenerate Typings for all SObjects from config
ftypes initThis command creates .ftypes file in the current work directory and adds default configuration for types generation
ftypesftypes add AccountThis command adds Account object to config list and generates Types for account object
ftypes refresh AccountThis command regenerates Account.ts in the specified output directory.
ftypes listLists all Salesforce objects configured in config file(./ftypes/ftypesc.json).
ftypes set-target-org my-sf-orgSets defaultusername in ./ftypes/ftypesc.json to my-sf-org in your sfdx project.
ftypes configsDisplays the contents of the configuration file (./ftypes/ftypesc.json).
More features coming soon...