> ## Documentation Index
> Fetch the complete documentation index at: https://docs.devin.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 从 JavaScript 迁移到 TypeScript

> 将 JavaScript 代码库迁移到 TypeScript，以提升类型安全性和开发体验

export const PromptBlock = ({children, type, agent, intent, playbookId}) => {
  var utm = 'utm_source=docs&utm_medium=use-case-gallery&utm_campaign=prompt-block';
  var tag = 'docs-use-case-gallery';
  var agentParams = (agent ? '&agent=' + agent : '') + (intent ? '&intent=' + intent : '') + (playbookId ? '&playbookId=' + playbookId : '');
  var label = type === 'schedule' ? 'Schedule in Devin' : type === 'playbook' ? 'Create Playbook' : type === 'knowledge' ? 'Add to Knowledge' : agent === 'advanced' ? 'Try in Devin' : agent === 'dana' ? 'Try in Dana' : agent === 'ada' ? 'Try in Ask Devin' : 'Try in Devin';
  var buildUrl = function (text) {
    var encoded = encodeURIComponent(text);
    if (type === 'schedule') return 'https://app.devin.ai/settings/schedules/create?' + utm + agentParams + '&prompt=' + encoded;
    if (type === 'playbook') return 'https://app.devin.ai/settings/playbooks/create?' + utm + '&body=' + encoded;
    if (type === 'knowledge') return 'https://app.devin.ai/knowledge?' + utm + '&body=' + encoded;
    if (agent === 'ada') return 'https://app.devin.ai/search?' + utm + '&noSubmit=true&prompt=' + encoded;
    return 'https://app.devin.ai/?tags=' + tag + '&' + utm + agentParams + '&prompt=' + encoded;
  };
  const ref = React.useRef(null);
  const [href, setHref] = React.useState('#');
  React.useEffect(() => {
    if (!ref.current) return;
    var codeEl = ref.current.querySelector('pre code');
    if (codeEl) {
      var text = codeEl.textContent.trim();
      if (text) setHref(buildUrl(text));
    }
    var header = ref.current.querySelector('[data-component-part="code-block-header"]');
    if (header && !header.querySelector('.prompt-block-devin-link')) {
      var link = document.createElement('a');
      link.href = href;
      link.target = '_blank';
      link.rel = 'noopener noreferrer';
      link.className = 'prompt-block-devin-link';
      link.style.cssText = 'display:inline-flex;align-items:center;gap:6px;text-decoration:none;color:#fff;font-size:11px;font-weight:500;padding:4px 10px;border-radius:6px;white-space:nowrap;background:#317CFF;transition:background 0.2s;margin-left:8px;';
      link.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg> ' + label;
      link.onmouseenter = function () {
        link.style.background = '#2968D9';
      };
      link.onmouseleave = function () {
        link.style.background = '#317CFF';
      };
      header.appendChild(link);
    }
    var existingLink = ref.current.querySelector('.prompt-block-devin-link');
    if (existingLink && href !== '#') existingLink.href = href;
  });
  return <div className="prompt-block" ref={ref}>{children}</div>;
};

<div id="overview">
  ## 概览
</div>

Devin 可以帮助将 JavaScript 代码库迁移到 TypeScript，提升类型安全性、增强代码可维护性，并改善开发者体验。无论你要转换的是一个小型库还是大型应用程序，Devin 都可以系统地添加类型标注、解决类型错误，并确保你的代码库充分发挥 TypeScript 的全部潜力。

<iframe width="840" height="473" src="https://www.youtube.com/embed/p_cvVcm0Zhk" title="YouTube 视频播放器" className="max-w-full" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen />

<div id="why-migrate-to-typescript">
  ## 为什么要迁移到 TypeScript？
</div>

<div id="developer-experience-benefits">
  ### 开发者体验优势
</div>

* **类型安全**：在编译时而非运行时捕获错误
* **更好的 IDE 支持**：增强自动补全、重构和导航功能
* **自文档化代码**：类型可作为内联文档
* **更容易的重构**：在类型检查的保障下放心地进行修改

<div id="code-quality-improvements">
  ### 代码质量提升
</div>

* **减少错误**：类型系统可避免常见的 JavaScript 错误
* **更易维护**：模块之间的接口和约定更加清晰
* **改善协作**：类型让代码意图对团队成员更加明确
* **增强工具支持**：可使用高级静态分析和 lint 工具

<div id="common-migration-scenarios">
  ## 常见迁移场景
</div>

<div id="gradual-migration">
  ### 渐进式迁移
</div>

* 逐步将 `.js` 文件转换为 `.ts` 文件
* 在混合代码库中使用 `allowJs` 和 `checkJs`
* 优先迁移高价值模块
* 在迁移过程中保持向后兼容性

<div id="library-and-framework-migrations">
  ### 库和框架迁移
</div>

* 具有正确 Prop 类型的 React 组件
* 具有类型安全 API 的 Node.js 后端
* 具有类型安全中间件的 Express 应用程序
* 支持 TypeScript 的 Vue.js 应用程序

<div id="configuration-and-tooling">
  ### 配置和工具链
</div>

* 在 `tsconfig.json` 中设置合适的编译选项
* 配置构建工具（Webpack、Vite 等）
* 更新测试框架以支持 TypeScript
* 与现有 CI/CD 流水线集成

<div id="additional-resources">
  ## 其他资源
</div>

* [TypeScript 文档](https://www.typescriptlang.org/docs/)
* [TypeScript 迁移指南](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html)
* [Devin Playbooks](/zh/product-guides/creating-playbooks) - 创建可复用的迁移工作流程
* [Devin Knowledge](/zh/product-guides/knowledge) - 存储适用于项目的类型模式

<div id="related-use-cases">
  ## 相关用例
</div>

* [测试与重构](/zh/use-cases/testing-refactoring)
* [迁移与现代化](/zh/use-cases/migration-modernization)
