Experience Lwc Typescript Migrate

作者 forcedotcome5164d94d751无许可证1K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库昨天更新

Use when converting an existing JavaScript Lightning Web Component (.js, .html, .css) to TypeScript with full type annotations and a matching `.d.ts` file that exposes only the component's `@api` surface. TRIGGER when the user says "convert LWC to TypeScript", "migrate LWC to TS", "rename .js to .ts for this component", "add types to my LWC", "generate .d.ts for this LWC", "type-annotate @api properties", or "produce declare module 'c/componentName' definitions". DO NOT TRIGGER when the user is authoring a brand-new LWC from scratch (use experience-lwc-generate), generating Jest tests for an existing LWC (use experience-lwc-generate), or migrating an Aura component to LWC.

AI 生成的概览

将现有的 JavaScript Lightning Web Component 迁移为 TypeScript,补齐类型并生成仅暴露公开接口的 .d.ts 文件。

功能
把现有的 Lightning Web Component 组件包从 JavaScript 转换为 TypeScript:使用 git mv 重命名 .js 文件,为 .ts 实现添加类型标注,并生成只暴露组件 @api 成员的配套 .d.ts 文件。它还会运行 tsc 编译、执行组件现有的 Jest 测试,并用随附脚本查找该组件的使用方,以核对它们期望的类型是否仍然匹配。最终产出是带类型的 .ts 实现以及 c/componentName 的模块声明。
适用场景
当现有 LWC 需要从 .js 迁移到 .ts、需要 .d.ts 以便其他组件或 TypeScript 宿主安全导入该组件,或已重命名为 .ts 的组件仍缺少完整类型标注时使用。它不适用于从零编写新 LWC、生成 Jest 测试或将 Aura 组件迁移为 LWC。
运行要求
需要一个当前能在 JavaScript 下正常构建和运行的 LWC、git(用于 git mv)、jq,以及接入构建流程的 TypeScript 编译器(tsc)。该技能附带可执行脚本(查找使用方的 shell 脚本和 TypeScript 模板资源),并会读取 sfdx-project.json 来解析搜索路径。
<!-- adk-managed-skill -->

Converting LWC to TypeScript

Convert a Lightning Web Component bundle from JavaScript to TypeScript. The deliverable is a fully-typed .ts implementation plus a .d.ts file that only exposes @api members (the public surface other LWCs consume).

When to Use This Skill

  • User wants to migrate a single component or a folder of components from .js to .ts.
  • User needs a .d.ts for an existing LWC so other components (or an external TypeScript host) can import it safely.
  • User is adding type annotations to an already-renamed .ts LWC that hasn't been properly typed yet.
  • User wants JSDoc-style type hints upgraded to real TypeScript types.

Prerequisites

  • The component builds and runs correctly in JavaScript today.
  • git is available (the rename must preserve history via git mv).
  • A TypeScript compiler is wired into the build (either the SFDX TS pipeline or a standalone tsc step).

Workflow

Step 1 — Read the component

Open every file in the bundle:

text
componentName/├── componentName.js├── componentName.html├── componentName.css└── (possibly) __tests__/, __utam__/, existing .d.ts

Understand:

  • What extends LightningElement? What is the class name?
  • Which fields and methods carry the @api decorator?
  • Which properties/methods have existing JSDoc (use as a type hint starting point, but validate against actual usage — JSDoc lies).
  • Which parameters / return types can you infer from how the code is called internally?

Step 2 — Rename .js → .ts using git mv

bash
git mv componentName/componentName.js componentName/componentName.ts

Repeat for any helper .js files in the bundle (unless they're already .ts). Never plain mv — that loses the history link TypeScript reviewers rely on.

Step 3 — Add type annotations in the .ts

Apply types in this priority order so you stop as soon as the public contract is solid:

  1. @api properties and methods first. Generate JSDoc if it's missing, then translate JSDoc types to TS syntax (string, number, boolean, Promise<T>). Validate each JSDoc claim against the code before trusting it.
  2. Complex shapes become interface or type aliases — not inline shapes repeated everywhere.
  3. Optional members use ? only when the value is genuinely allowed to be undefined. Do not sprinkle ? defensively.
  4. Private/internal state — still type it, but don't export the types. Use private for members that must never be touched by consumers.
  5. Event handlers — prefer precise DOM event types:
    • MouseEvent for onclick (and other click-like handlers). click is dispatched as a MouseEvent — including keyboard-activated clicks — so typing it as PointerEvent would let handlers rely on pointer-only fields (pointerType, pressure, etc.) that are undefined in those cases.
    • PointerEvent for onpointerdown / onpointerup / onpointermove and other pointer* handlers where pointer-specific fields are actually meaningful.
    • CustomEvent<{ detail: ... }> for LWC custom events.
    • Event is the last resort; document why when using it.
  6. Async methods always return Promise<T> — never bare T.
  7. Avoid any. If you genuinely can't type something, use unknown and narrow with a type guard.
Reference patterns

Load [[assets/type-patterns.ts|assets/type-patterns.ts]] as an inline example covering property types, method types, and event handler types.

Step 4 — Generate the .d.ts

Create componentName.d.ts next to the .ts. It must:

  • Contain only @api members — no private state, no internal methods, no lifecycle hooks unless they are themselves @api.
  • Preserve @api JSDoc verbatim (including @type, @required, @default, @param, @returns tags) directly above each declaration.
  • Declare the LWC module namespace c/componentName (or the org's namespace if different).

Template: load [[assets/dts-template.ts|assets/dts-template.ts]] as the starting .d.ts shape.

If the component has no @api members, still produce the module declaration with a comment explaining there's no public surface — don't skip the file.

Step 5 — Compile and test

  • Run the TypeScript compiler (tsc --noEmit or the build's equivalent). Resolve every error before calling it done; no @ts-ignore patches.
  • Run the component's existing Jest tests. The behavior should be identical.
  • Run the bundled consumer-finder unconditionally — empty output is a valid result, not a reason to skip. The script resolves the search paths from sfdx-project.json's packageDirectories (or falls back to <project-root>), rejects any entry that escapes the project root, and performs the LWC-import search internally so the invocation is fully deterministic:
bash
"<skill_dir>/scripts/find-consumers.sh" "<project-root>" "<componentName>"

For each match, confirm the consumer's expected types still align with the new .d.ts public surface.

Step 6 — Expected final bundle shape

text
componentName/├── componentName.ts          # Main TypeScript implementation├── componentName.html        # Template (unchanged)├── componentName.css         # Styles (unchanged)└── componentName.d.ts        # Type definitions (new)

Verification Checklist

Before conversion:

  • Component is valid JS and all tests pass.
  • You've identified every @api member and its intended type.

After conversion:

  • git mv was used so history is preserved.
  • Every variable and parameter in the .ts has a concrete type (no implicit any).
  • Complex object shapes live in interface / type aliases, not inline repeats.
  • Optional ? is only on genuinely optional fields.
  • .d.ts exists, declares c/componentName, extends LightningElement, includes only @api members.
  • Every @api JSDoc is preserved verbatim in the .d.ts.
  • tsc passes with zero errors; no @ts-ignore or any used as a workaround.
  • Jest tests still pass.

Common Pitfalls

  • Using any to silence errors. Solve the actual type instead. If the value is truly unknown, use unknown + a type guard.
  • Including private members in the .d.ts. The .d.ts is the public contract. Internal lifecycle and helpers must not leak.
  • Losing JSDoc during the rename. Scan before and after — JSDoc comments on @api members must appear in both the .ts and .d.ts.
  • Skipping git mv. Makes review miserable and confuses blame.
  • Forgetting async return types. foo() with an async keyword always returns a Promise. Declare it.
  • Typing onclick as PointerEvent. click is a MouseEvent (keyboard-triggered clicks included), so PointerEvent fields like pointerType are undefined for those events. Type onclick as MouseEvent; reserve PointerEvent for onpointer* handlers. Use MouseEvent | TouchEvent only when the code branches on TouchEvent distinctly.

Support Resources

来源与署名

来源:forcedotcom/sf-skills位于skills/experience-lwc-typescript-migrate提交e5164d9

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架

更多来自 forcedotcom/sf-skills 的技能

Service Itsm Teams Itservice Configure

forcedotcom

Configure the "Set Up Salesforce IT Service" checklist for Microsoft Teams Employee Service (ITSM) — the employee side, covering app enablement, marketplace install guidance, user access assignment, and Digital Experience Site selection. Use this for: 'turn on Salesforce IT Service', 'set up IT Service on Teams', 'assign Teams for Employee permission set', 'give employees access to Teams for Employee Service', 'manage user access for Teams ITSM', 'grant users the permission sets needed for Teams Employee Service', 'select a digital experience site for Teams', 'install Salesforce IT Service app on Teams', or any request to complete the IT Service half of the Teams ITSM Go page checklist (including the Manage User Access step). DO NOT TRIGGER for the base Teams Salesforce Go page toggle or Azure/Entra app setup (service-itsm-teams-configure) or for the IT Desk/fulfiller half of the checklist (service-itsm-teams-itdesk-configure).

待分类1K昨天更新

Service Itsm Teams Coordinate

forcedotcom

End-to-end autopilot orchestrator for setting up Microsoft Teams integration in Salesforce Service Cloud ITSM — runs the whole flow (enable the Teams for Employee Service Go feature, register the Microsoft Entra app, populate Named Credentials, configure the IT Desk and IT Service checklists, turn on Swarming, and optionally embed the Agentforce agent) in one continuous pass, stopping only at the points a human must act. Use when the user asks to set up Microsoft Teams for ITSM end to end, 'set up teams for it service', 'do the whole teams itsm setup', 'configure microsoft teams for employee service', or wants a guided Teams ITSM walkthrough. Delegates each stage to a specialized child skill while driving the sequence itself. DO NOT TRIGGER when the user asks to enable Teams alone, configure just the IT Desk or IT Service checklist alone, or enable Swarming alone — delegate directly to the specific child skill in those cases.

待分类1K昨天更新

Service Itsm Teams Itdesk Configure

forcedotcom

Configure the "Set Up Salesforce IT Desk" checklist for Microsoft Teams Employee Service (ITSM) — the fulfiller/agent side, covering app enablement, marketplace install guidance, user access assignment, and Swarming collaboration-tool setup. Use this for: 'turn on Salesforce IT Desk', 'set up IT Desk on Teams', 'assign Teams for IT Desk permission set', 'set Teams as collaboration tool for swarming', 'install Salesforce IT Desk app on Teams', or any request to complete the IT Desk half of the Teams ITSM Go page checklist. DO NOT TRIGGER for the base Teams Salesforce Go page toggle or Azure/Entra app setup (service-itsm-teams-configure) or for the IT Service/employee half of the checklist (service-itsm-teams-itservice-configure).

待分类1K昨天更新

Service Itsm Teams Debug

forcedotcom

通过针对 Salesforce 组织运行通过/失败配置检查清单,诊断 Microsoft Teams 员工服务(ITSM)配置故障。

DevOps & Cloud1K昨天更新

Service Itsm Teams Employee Agent Configure

forcedotcom

Configure the embedded Agentforce Employee Agent so it replies inside the Microsoft Teams ITSM custom client ('Salesforce Employee Assist' / 'Ask AI Agent'). Use this for: 'set up employee agent in Teams', 'embed Agentforce agent in Teams', 'make the IT Service Employee Agent reply in Teams', 'Teams Ask AI Agent not responding', 'agent joins then leaves without replying', 'configure MIAW deployment for Teams employee agent', 'Teams embedded messaging agent setup'. Builds the whole stack headlessly (zero Setup-UI clicks): the Web messaging channel with User Verification ON, the Enhanced Chat User Verification Key Set (JWKS_URL) it requires, the Teams_AgentForce custom-client deployment, the routing flow to the agent, and the Agent Access permission set that lets the portal user reach the agent. DO NOT TRIGGER for enabling the Teams feature Salesforce Go page toggle (service-itsm-teams-configure) or for configuring notification preferences.

待分类1K昨天更新

Service Itsm Swarming Configure

forcedotcom

通过 Connect API 调用启用 Salesforce Swarming ITSM 功能,并将协作工具设为 Teams。

DevOps & Cloud1K昨天更新