从 JavaScript 迁移

TypeScript 并不存在于真空中。它是在考虑 JavaScript 生态系统的情况下构建的,今天存在很多 JavaScript。将 JavaScript 代码库转换为 TypeScript 虽然有些乏味,但通常并不具有挑战性。在本教程中,我们将了解如何开始。我们假设您已经阅读了足够多的手册来编写新的 TypeScript 代码。

如果您要转换 React 项目,我们建议您先查看 React 转换指南

设置目录

如果您使用纯 JavaScript 编写,您很可能直接运行 JavaScript,其中 .js 文件位于 srclibdist 目录中,然后按需要运行。

如果是这种情况,您编写的文件将用作 TypeScript 的输入,您将运行它产生的输出。在 JS 到 TS 的迁移过程中,我们需要分离输入文件以防止 TypeScript 覆盖它们。如果您的输出文件需要驻留在特定目录中,那么该目录就是您的输出目录。

您可能还会在 JavaScript 上运行一些中间步骤,例如捆绑或使用其他转译器(如 Babel)。在这种情况下,您可能已经设置了这样的文件夹结构。

95RJhN6UPNTpIwMpDAW6BqSaOEEwi7bA3prOxEsGqhaZKM6wf85cKqkTboZNMIh285vwmNsFqE46dekaKTaX0w==

projectRoot
├── src
│   ├── file1.js
│   └── file2.js
├── built
└── tsconfig.json

HS/pW/w0FRa/Nrko1jsmIGzPdUpj5Nrs7OBQhXhi970Qf6u7a+fUjnnm+2UDTA1VwPrHQpYF8jOmYjpwMq+yihpAWH+rpDYFli+F2+adXQ+58/akmm/IzWulEhnXSGMl87n7P/ETLa/W1we7nAvOYvtHtsC0KZ9vJomAgHoGyWbh28kZgMeKX4rxSFFdycBSod0/t1AWi3JQ7mpBX+XMOyvn4C+nQAOBs5Ccr82o25JZ5X186dI0OhWlWST4ZAn0gt9bBYmog4/SOHCZfN8wB3Bo04BIwk2qyRj4oQrPkTM=

编写配置文件

lenrz/QP8O8bUqL2AoP9ObWUZFOP6E/Z/sIf2w75azrgsTmsdTiVDclUkApAakP7m3KxmC48oktinNhRkZ7BYniSjspYraaMKfDZBR+LgbGGpiGuUqh6LxwSbtwW9Y3sGc0j2NP9rfEMFR5BSImWfzPnAkHQs4CxYAr4A/lozT06QnKtYCnPwlTcdlbVMEwuqCs8qaUTFkjzGdoxjHZLGh9G17aIyGcXGx1Glt4NCS++T9soLHqnh3nANVJf2OUs6q/v+KraE+U3k94afTeV+PqV4wRnu1DCK+iJmOFGkKQ=

{
  "compilerOptions": {
    "outDir": "./built",
    "allowJs": true,
    "target": "es5"
  },
  "include": ["./src/**/*"]
}

OBq/LLCwWOrPW6fqHnxf2rwV4PjvS1ZyPYnLbrXQBAuFpeW5K/lGybKfWj88UHvdEK1mpI141reswG5AeTbohw==

  1. 读入 src 目录(带有 include)中它理解的任何文件。
  2. 接受 JavaScript 文件作为输入(使用 allowJs)。
  3. 发出 built(带有 outDir)中的所有输出文件。
  4. 将较新的 JavaScript 结构转换为旧版本,如 ECMAScript 5(使用 target)。

gGCtq+QRLNW0T8y994/Y9aR3p2v7Fddc7KZJ1jTVwcwJ7CVRolnZvDZOhURPeTGKKkZ9NlGaeDGgDxpn77C2xBMJR998aRaPdCDG1JY4+iztvQbR377B0fWpVhC0BRhl7TBenX+q26RDRDbLJwg4HafcAb8B8d6TyKccARn9zr7n3xMjcY9tiKc6tFm81y7+2T2u70V1FMpVMUD85p5M8GJTTx9T3kRRm8CVdcQMJ3ZmICbyQIsnPq2JKiDl+jfkTJhCgeq4eXJl9xzJZfRn1G+/7rNh83X6wP3s9tANT0+FrrSUd7J324XjlVsgwl2mo2BWg3Tn/gNPuxOUtfOmD5fa8Mw1hpibjY5xYF8Cc/jtY5/9naEpRXrd3C/s6yxc

早期福利

9SkfIm13r7TIwCgl03rEGu/KlxY6IpMS8Rj5InW5wg18sH0wqLDx21o5k91aEOto9NkQFrA1LvEEcc1Pxyh4eC5gxMPK74SemdPfwJSpHkR1UvaOuKBWB69IIp025vj1Z+tc6xJ049u2+eGU7P0m4ip8H7O9M/CUy2p/UUK+TTQ0h8yru5t8NThvpwl6p6RNekifNNeGc/u4Q8NtXU0+2kH+D1J6b++KSKBCH0PqODbUT8O63TQfS9BubL970BRT4fYB93EEfjx4kLtAaE4y2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TrHmh+7ctrKDQthK5i6itPktIHy4uyjasKZu8dDt5wC/ipo=

与构建工具集成

hOhX8KoDOTq/yZx7EuPVv1kHjvq2HAdyQ4EHbqJ941Te8t3hPUkTmIy9WCFhYD6WIzl1qADigQbRxotRdc5MKhKOPlOXLGtltZ1Wq4OfLHOaMcfkHnpxaHIy1reYYpMVuS4rNtCFt7LViTp7YlA3lXVmX1cpRJbVPynb9NP3CAyDnMuvup+9SONIvuEzLgyMd3EP/GcVVYa6vtgfTSibR0MYWt0NbvbhPO5YEATz4UA=

Gulp

VThNlpsRtXKxIgljdHs5hoN6zvRcGDbieaqlZE7qA9telKIYnWHtU3jyUn8GA8n9WvZdfDo+M1DyCu8UlOeewC1fgzI9WoIm1+YTMMTzkIyZepi4eI/LlfYGv4kLn5bySJCmcqoJyPsHQLT04MdKab2D3ruf0YLOTqvB4v+7u8om6IO4Lp9/f/oK/kw3fybKMYu78gDqWnAFJccmuKf8e5lN8Lb0UP5oEHmL0FZcCQYF/aLxiJEY9cQECzzpxv8eTgCUrmqpvlB44vweMxocNhxfOymJD43I0/mH6jVNt8+471Y3D38kbzdLvnJU6EzPEtiBZ/62ajP75L8BrLKRoI0kfiXVxVWntjSRFdwpGTs=

Webpack

5xStqKyDO5AM+qSjz+gW0atmc26KEJM8c3abT05pADOtz8OMZLlP3NsgpgjSaXKHviiv7RblKMRNCb6TbR2UBlotuI23N/jtWzbGtnewy8fSiznGoU/vWMZKqYkpcCk5+cO6HPl4dJsor/Gs27MSycgs2jo75AFPWEFAoShgAjcUb/QDysiDITysYM970Fb25eQg6kF6c03/UqXP2dERbuGM5gOIQd38HJBKTk9HP54=

npm install ts-loader source-map-loader

H2PhClds36TuBwH89toRfvotbzgZBE8rdGabNXiBV2f4IUditmL9IwbFqCMNdBMtgdg/whG80Djli9xPZEX9n4MLv/TbEUKWXGyG9850YN4=

module.exports = {
  entry: "./src/index.ts",
  output: {
    filename: "./dist/bundle.js",
  },

  // Enable sourcemaps for debugging webpack's output.
  devtool: "source-map",

  resolve: {
    // Add '.ts' and '.tsx' as resolvable extensions.
    extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"],
  },

  module: {
    rules: [
      // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
      { test: /\.tsx?$/, loader: "ts-loader" },

      // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
      { test: /\.js$/, loader: "source-map-loader" },
    ],
  },

  // Other options...
};

WGJlVaQNvtVuwgjMkLImvaaWKQFc4qojeKzDONfKsS95bOXwK5r5e1hLMo6HsCGHD9PS5uDDiHnQkwGf+qKRphhnuhfCZFUTvNQuyTBNJhV6AVM4Dopm9x/GisEL5WOlucw+kcWBkUsJuIpTPytSaMez6kjcl6+6FiQzvUyPWV0=

9iP/PUV5v8to372hsChcW70ji86dFd3m2dIvAg5nJ5YvMyrJFV/FNb+atbOT4uAqeFJGo6f6ZJxB8oYO5Emh80cxj81tiTgdBpabgg9uRClbR/tUczB1//DU1Ua4SzIJFoB2EYAGcRNgQyL2DD7iok+zxZeYSyaCo9XkoWgaAPJVykuPWvjrDgZWd/+pku0E/bS8VJ9eBRFDW7blBicFgurUrkHLItqeiBndne2QWVm/xBylbDZUJWotehsZHadjHLLgSmFWWJ/nvmoY6BWRh3QPQ9j8x7id3KhDOfXzz1+3NDFTkxDaB+zyfXjxD+cIHACbg16bEwYUGQd0tC9c+w==

jZldo0FTkRpWRKtSoAZvGeW/b6EYD1QAdRxorVgG3aJHxDgmXzIQg5j+2JMm3b+wt5Of3YXnIosUQtvEGhDTTQbnQOo5HSUM1Z454VmTxbJBzFYRpy/GwPWCyA1ivxQ/u1g2RXaAcLRRvxrNqJtpcH0hNSsyqh/zOp/oJHK4FK7m54ZtULg2NxscCFnXWFHN

移动到 TypeScript 文件

g4zB15+1MqjLna2iiIc7KVEcp1K4KzzFV1AiuEc0Ika1iPA4lCqeELmLbz0LOQvYi/lYCA7rC7yXJHl57D9xELmu/9fbsOoLberToP6BlIEeyvqB2uu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9zD0LqubOpAevkGu9/YhEwOAxP6zGmyRw2no/OpSpQ5nprLVt3exW9Vlj1Gklmm4F7nibwzCahXzqI4+buDKwPrpTzBWKYTlE1OVfQyt0gRVzQND8HGQ3kJKRXEVXjme2E2Wl4gXB4DI3B+KgDW5wK/PrXgNkEZi/n5TH3yHfGOWRx8677sM3gUoDc4/YIdjTlT4UMGsWJ4yumC3CA/oAmVFxiTbmwb6q5VogFgPaCIM=

从模块导入

j242+Oaz6l/ddtG2gvSHv7DMR9gEZXQXXQ5SjUUJApt+8Bh1m4hwVcJziY4PgynbMgz/9PVFH5ccXOf9RCUNx1w/jayz8l6acBwWR7PYeK+5DjM28rFs7pEQ8znBA47ZuSy9+joumdhsoSZbIr9ZSVkCPI0yGWuw3q2+o9Qobdieas8BTD4dyLzkkzYTofgsQH2khWWdka7VFiUtlmC+dyo4Eyi3OAutlPHYVyEv9YSo+b6Qwq68MaNJuy0CO7tUkA/BeCmHiQtAoRi9/0Y2arww8VAiJpEev93MBNKQGnrxcLCIT8805kHgTDbaFjNKF05DmvKdrLkbkgfAcUYpxA==

// For Node/CommonJS
declare function require(path: string): any;

roawiAY3FwQM1etbglDVAQ==

// For RequireJS/AMD
declare function define(...args: any[]): any;

eS3qJ66HO2ND9ebNk6LMiZerAbWR15OpQpWDwA+j9pcQEwihGvIijPtlp9HzDhLv2oPB91g0Jbz7xxy0fQ39R+rusQvDDrAqIsBIaZCKOZo=

q1ufsuZwdp5cim8fdpzQVt+GcS11Vw2ZMjkVeeePZyB57PFICe+VACDTr8QdcAxPktZTsU6B4tuCu1IrMO9MSWHjGyKclFSr+2HuT+HcPuPg4SSs7sKIihAGPQ0pil6VDDzsINLT2ucw8RGYP8SCYO19KEfZ/lUsZiiU7dBWzozWhHGMi5SjqwxVJsM0plRK0R5f0INr+soUtOLHBJRFQtIdvCzIJleDp9EEQQ2dfmdTerTKHIAV6sgJ5sda4YPMG1zR8vm5KSlplhz/tYY65yWrZaUUtk0FUh6IqFwQdytH77OR1hG2MKd5H68ryaiOEMWWEEFBGB0su8OXXwHoMw==

gmId+I2xx9aesVh5coVqiE2Ety5NDcUv00gpj2RfoI9LyCVDeZF8n2tCL1e/hzEV

var foo = require("foo");

foo.doStuff();

ZTbgLRgVeSSAqzAe6i54N6EW5CPd1A29Z3R7JaQ94M94rKOTgt0KOSaWS+kJj/ks

define(["foo"], function (foo) {
  foo.doStuff();
});

Mz3R/7nYui7tuHyhqI61TifVEudNtPx1a8VZAr/OdvK+c7toTCmFKOkztKRs5HMx

import foo = require("foo");

foo.doStuff();

获取声明文件

5GrmtHVsVxGiPNRXwB1e8oBFO+wjF0d6pAmaoaHT3rh0xIw8k+4Z/ZKEV9Z4M0OCZIvuosWenLAlE8wm4Rx4KIdzZSeZZd3zK2MEQJBeg1B/53nrj2bThWqcWOH55IHUxIDxjGnF5m1Z1Zpra2JA0cnI1KURF2BTCCqbSuEE1plxEnC+Ks6idAhEIJMB0dlILwjBBqxBLMt7UuvBK8Gisj8igXp9IEYWyRGovfBVTiWlsEku65aSN6H9AkhFtWAmq1CNIyIg/7slsDHD1jNL8/KZieG0IlEPUB5lmRtHNbXaAgPn5hy8ZpCaLZio1jYbejBhaGKqSPDcOv33hCZHVyVjNc6b7xXFz674cyWyU7+SVdYX1XV1LP9bYT7JjOAvYHU+XzoYKFG9Lq55cdKCOA==

npm install -S @types/lodash

KCnhitIZeth4RBOH5sFVLxK4gV41q/9uU9GChUc9fX+hPTO2a+H1JNDW4JDa+SLKTdRMsUvugaf8UvLNswPWxb3kAT5KBQrEofZytOw2aQXh1d7KtKzxXvu1sNo9z3IA3fuMUYLHGjFvMUnn6Ib+YzrCOpSJ+g9GJAqwuEvMSvmVt3/jhyiAehNZd15MoNQhS0fD23Qll06PlTOZaX1yWh1ILEc/FZsIkC0LL45JPYlHv8YvTZbDayQVFqLVYxF9

9GSinjuRXHyCIfxRX6lC5bY793cbQP1YzoqTMNUmUNTe4Xi048D+nY3n8E5hUmf//3E8qQ6ncY+Kt3Tj8DD4F3A0QP84IcaE16kpwdqek+U=

从模块导出

W362od/pX34wH49Ikxa4UQNF4u6lGOiIHdKdC1Ye91iOXuZR2fcWHNpQS5bajx7Mm1Cbh222gJz6UUX1xo+k1iaJZ6oyS0+B838qZcMbSo+/M/0sybZRAbuVDTCUvzofK3+Sb7QdK7+mxj8bUUhSniFE7nuG/ZOpwiTnv4OORwBeT4bjCtAdbNYyQ0GH/EGKt/PmcRbI6D4GFukYxEKNjQxspgmizLMzYfb++uTLN9ZhKl/ReOroi547cE/iiodGBvL5f6/+fKaI9NmS/9nrMQ==

module.exports.feedPets = function (pets) {
  // ...
};

9ElUbbtl6MM4HrKKF7tWIa6UCCbtTrVfBY5/IsSnAAU=

export function feedPets(pets) {
  // ...
}

RRWN7Rr4J1yKxxYVbCKAlxW32PCoy1AUyvoWihnqSoLU7QR7sjSJ+F9IsqfywZUFt8oLEJMe69maRvG3IE0KvvRl50/TArXqSu7FeEtD+hFlQG2gXR+EudyVAS77tzg+l/PpSgIwlsYTI3L6HrcqfjWwKWq0aa/Ym6hwr/py3hnf2FwpLzoQllcK8LFXxWe8

var express = require("express");
var app = express();

VExTfkKW6Z9fX2rfv0tH1sINYXkGH30AFYVvS+aXY+A=

function foo() {
  // ...
}
module.exports = foo;

FNORG0udSyAznQ4F9Frad0GcsvWPHGTuCvYdhmYSn0mPQaBQ9xsImUEqkhfhIFZetO5L8Ns8+F+Tmaf5XG2sWYZYIxOGsMzwd4e/s/HSnYoI/WsccPSleVm5xD3D/lxE

function foo() {
  // ...
}
export = foo;

参数太多/太少

UVE0POLXCZSbQe+U5I4ZU1dDEoLf8cYzrO1ifSwWmkf6+E9DxAckSQkVT6kMLsHIdn/uFDIrCXty3KlyNwTsDXEwoFqJ/Zf1jfTTTR337QKQatZ77Q7YLpCjpZr1ZvTat/xEDxPh2OuT2MNYqcQnnh1vPZFdfQBDN2sI17prEr8cE/LZezG+veSR8toHq8AMdFUpXn9OWpSPteXMHNo8/RUJ0s/FEDufW6bHBjpSars3y+mdGP+4c7TVN3NzRhibyMY3L9Wg7c40VQyOIBj3xrvco4mEgie7axuZuZL6de3oFAdbArtS9wN7AHvUtvKA

function myCoolFunction() {
  if (arguments.length == 2 && !Array.isArray(arguments[1])) {
    var f = arguments[0];
    var arr = arguments[1];
    // ...
  }
  // ...
}

myCoolFunction(
  function (x) {
    console.log(x);
  },
  [1, 2, 3, 4]
);
myCoolFunction(
  function (x) {
    console.log(x);
  },
  1,
  2,
  3,
  4
);

iyaE9/Qh8kfAlINFddd9ek/gHJM3la6uI8FdREG8JqXVbuBh5eyVl62yYrVk6hc2jzZEFREVxCO6ZcaH45GwrzDpkdQocV+8UaO7WL2zZjWlJOhASylqTF06xf7uwmQbY+pIL+yVQxgUpokBfDNROskxR6Urerm1+11VWSANVAZsL4hklGbwDeAnDgc1k7FUXH0gSFR998tjWn+aL3jR6A==

function myCoolFunction(f: (x: number) => void, nums: number[]): void;
function myCoolFunction(f: (x: number) => void, ...nums: number[]): void;
function myCoolFunction() {
  if (arguments.length == 2 && !Array.isArray(arguments[1])) {
    var f = arguments[0];
    var arr = arguments[1];
    // ...
  }
  // ...
}

HDw9ivDG7Mu2Sk/VNDcwSGBpaOqE68y0fYIZSerA7JM0gzB73K/Eyu5dKveP+8KY+Ke0tL85ObhOOSyQjbsbT/fpklDmcp6QgZb/yYc1YlNNtmWRLrFY8H5Us5uRiclBf/LaF0tDdSkU3MEW4fFaYFNx4Ib3XtqKtFJf39+jqdVuXXFUKerFgkJIKDkYeTE4vZigJ9Zty24qZ/REPXp22DbVrddHWiQ1II4YV2koQiwCFZ7BwzUGz3K75CHf4xWQ3nQlmSImj4oof0+mhJAsjY34Nk6RRUC2SbyssCUfbtmKs990YV96ACbVBzKeeqN6mgpgZhUMYhraXUQ+1eVMuKZ4ObEqvul7dAKj70f6amgU3dxkcZ9lQ5ADcyndFO6bLLo/OlyZ0GF/tIms6Dc20TnbGaAzzVbhevnxt1hBtFaOKNtHgU3r50MmxFscpSsamQWz+tTezz5GUcgwCQiEvveEVadTjSul6jnPQEZfYuyCKy3GpVmtVBH/WyQ3R14MZQErjje6xIJ8IAFXj25DHg==

顺序添加的属性

DZ+a7QCAw/AmA5d92Zg6WDdhldaam729+0pu4ch6Zk8sXdRNgTVdkgRtwZ4CAHd15SYAAH0A6Cnw0CuNfvyCEsORBPTMLeYjXB1xZBvaJQE=

var options = {};
options.color = "red";
options.volume = 11;

+1HRGp7VMIKEAydSmvUXIT8+pOIy7iqu51FQSd1mYpB28E6oGopvfK/47W+ZJy4cVqtmhlZ+7AYcooDICehd+TWrhv0ydDk7khB0ix38CBP11X0b25+Q4TIekm4QuTYQcyPOgmNiukNYUW/wyR14i69tY/LHeh1ckrFHih0oEo5VW71bNZR34pBW5AwDmZhog8FAa3hx5uviuBCLxef6AieVNv9bDHNrzMtJZp0X4nv+nUFROfRC1H9LdrwFVeikx9egmpxSmENPeVOreY37GL841TTnpbm4eQ+nQ3MFJRtfkb6JO+r5AlZw/iPaxjaacqsTtvhChgtxMvLYDqW7ZoX6fN6Z9/K4aOtcrfFj9ZE=

let options = {
  color: "red",
  volume: 11,
};

jyIpl2zVmuBK5TaMQud1q6F/ww0bpduXpL2PfOhvm2TEPCi4ItYGahzSUiGAEM00GVJR06tjjY4vIHVFPAfvusWNifXf2PBFaRgjh09yHxxTaP9pxLA2AHUPL+r5SFZS

interface Options {
  color: string;
  volume: number;
}

let options = {} as Options;
options.color = "red";
options.volume = 11;

PFIxXXLRnqoG75aKxYZihDGETUenLhnZpJdVhqXuzFpHNakUVUAUYbCmwVq4rZF4Rb7Fw1CTRocwnGNRPzfJmyLGNmbEDjpsHHtrX7uKARfLjP7JAp/mlYfvDEPp/F19YfTBwwp7GMHuXqk99XDEqzoHlcmEwE/3rTbsfDWXlFPlaWGLavoL7Zq0VjD6jzpR

any、Object 和 {}

mKq3KrwS2CSVCSXTft5ZVjARzbKogyrLt6pHvsGU2CjdjTuna6SKP/NC4r1xNoOASEye5+OIbjTPF8M5oSd43AOvr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EiyKvhdNP0TOSx3HNLyjk2j97Q2gV89JVfBNiIJtOq1CNx49+BenFcnDZkS1mqdHnKqumrJHNzkLxC/tRql7vBoP7BmK0MRb+6aOGOowoDhEEETzL84oBK9HFr22kBonOO/DZ2juXMxNDiVEQqiGwPAXxDkqM0hIkiQD48lvxPQ8fbmIjb8AdsY4BETjGcxWAi0o4giqnz1F4/ZdOzJLgbJN+OF/i8G7pRq7Om/q8w9yRWWntb8BPFQnG/hVxWpM9QHFMBdpMcEsIxAqoGQR+Kp2f57TFeb658yZRt9PkVJWk+Z4lO5NFlQx0kDvlgHOJEevLc1srNDHH85daC1nvi5k52UsXN0XGpOm5KIrTjLB4mlsRtAT2pP6cWtOb3x6uqfpFp5lkrr4oP1l2MIMU3FSmPZIZukmuOQ

kB/Avo5FO+aaEBUPQQfnEHM+w1zXnMthPAfDACCjL3F15+lmviJRj0uUQVYpFQ7LYlk0TMhQa/0KE42ns8mQ0pD50YvRU0KzwKHsAd22EzXpxlBpPWk1r87q9M1aMx2s/I6Ex4oJA7CxiCkinjXXY5j8sIlFM7CKeEK/xWy+ZP5Afaw39ominxpL7LCUeDGHfGBjdVz1DQzs9NFfY4A10T/LAlcpJxhDzSQ55zvFj3CbNvIziadLeqARLkZ/ffIji5ByGo3rV6heHGg2BTpRdQ==

declare var foo: string[] | null;

foo.length; // error - 'foo' is possibly 'null'

foo!.length; // okay - 'foo!' just has type 'string[]'

GNcs7L/5/HtomQp1kLoqY8zHwr4Y55WqbuKlZAjPkKK1BzIAzYFdw3yOmbM+oVUYgM+ZzxaitEpyaZ95NjOLuCbzvVEASVm+i54Lyx5KjacqWACAz9l9k5mlJY0sWfEFvugzU8PoHXlzdtFqDHPP9fjzhlbKGB82v2uzVjRGxfdkqwTJaRISRScRIpXM2H8eQO2iDvMqOtuzb9+5FlcUVBMqlPfVsx6mh0PABytcG++o0kZYnfk6R4aRpR45RIlAFnONPwBe8xZadhol3AJpZqtNvjiPnYOnaYmnyKQHZcZ1At7I6o5X9XBwMBXZaNJI

this 没有隐式 any

C5+YtNzVw8ETh28TAbbqCXqUV3ExNmsNNm4OwvdtHuSBNg3DSbeSt0CtYCcpmhNFkvlwM56rE0Q5IDXeRETMCLo2XBA6vNbKsn+Aml33Vqyny4kuiWi1BtPQeKoi55HxdBx28xG8aKDdjIqqavXh8MoQhJzpsxEkvw/7mrOX6rVh8TY0hGOBgQMmXvmbUYOcDlk1Lyj57sb6qXroGoiyGpxfzbPYL2xZLlV7rFHVPcoGaEk/P6Kd0p0IEUpM2blxumU0Qu0MyC2YOhkhyf5S3A==

class Point {
  constructor(public x, public y) {}
  getDistance(p: Point) {
    let dx = p.x - this.x;
    let dy = p.y - this.y;
    return Math.sqrt(dx ** 2 + dy ** 2);
  }
}
// ...

// Reopen the interface.
interface Point {
  distanceFromOrigin(): number;
}
Point.prototype.distanceFromOrigin = function () {
  return this.getDistance({ x: 0, y: 0 });
};

CVYGngnP/EwhF9tqSFPc2NqBc3WlVpJ9+KgXaGdI2xF9wQUIztvuKOcLCoSzfAr0G1AsYCYkIvFKZTDyyQhkMidv4pwQ96kCC0FnsvlE2k03njMoKOI4dy7LOZeDyufyOdK9+/YZBHQ17rvJGm2P+0LfnKr2C8HoxPGMVH9la0RRjPmUjV6xIlPmJ4Dt3Q+YAHsS/CdC5mT0J+imSB0hiCRJClAkMFPc1O9tfswH+M5nolyu7Dhb/AlTCz9zvOE/BEIpHBC1yDNiZ+vuL5HwMX8/QRm372R8HCaNbPyzGSoVFCSXGxJffJf86Q4xysrArFDYp41uEa2XkG3nZxzMAb7b9aKo74EFKVKoZxDDp6f463nhieTH09uX8PoRZlhsNftI2HjOJv5996ZOVMyrrf8gOMkVrj67sNMMBUg4YuvP6amYxahRzY+mpHNGr3n9/o0bgwQZg214ZzeYl7hZ2LRPWFDyKMGBRwzI1Eq1rOHsnYuBFrWeiUVOw/hsY47+rpAyZOTFP78IJR595aRuk7SIv3FuD4NLIqK/qiGPupcziSK4JpHNhHfNMMiv9gaBNn4TLaScXbA+HDCt6jzjSg+a6GJZNotnepRaMCE7baQ=

Point.prototype.distanceFromOrigin = function (this: Point) {
  return this.getDistance({ x: 0, y: 0 });
};