日常类型

在本章中,我们将介绍 JavaScript 代码中一些最常见的值类型,并解释在 TypeScript 中描述这些类型的相应方法。 这不是一个详尽的列表,未来的章节将描述更多命名和使用其他类型的方法。

类型也可以出现在更多的地方,而不仅仅是类型注释。 当我们了解类型本身时,我们还将了解可以引用这些类型以形成新结构的地方。

我们将首先回顾您在编写 JavaScript 或 TypeScript 代码时可能遇到的最基本和最常见的类型。 这些稍后将形成更复杂类型的核心构建块。

原语:string、number 和 boolean

JavaScript 有三个非常常用的 原语stringnumberboolean。 每个在 TypeScript 中都有对应的类型。 如您所料,如果您对这些类型的值使用 JavaScript typeof 运算符,这些名称与您看到的名称相同:

  • string 表示字符串值,如 "Hello, world"
  • number 代表像 42 这样的数字。JavaScript 对整数没有特殊的运行时值,因此没有等价于 intfloat - 一切都只是 number
  • boolean 代表 truefalse 这两个值

j9iB2IkudLPXdOvpnsGRQEuE/j9r0gkb5GN6QBPcp/4eqkueZxcs26U5VVEAYAffYYyP/MLMYJ+wk2FSUkO8692Qd6omIDHayd4K5Owf+Ma4PsI9RnW+xMKB1S6IfUm8xBlO5yYpmqM08P6s5mYmEzgGjVoikHgZjt3Q35XTwyi8FY/V8gMy+qJ7IogVtq6MSxDvz7QUmTptu0+TPU/uPBDTpnsERCOP1F+LCYYbouPsmz27AQzuZ/ReYfqGBxHOGwVTKbdapu3nkSAzX48wqXQ6rY/+4EpMah7DCAXOvx6XxWdcblPC47vWdeBHUJ3rrjDxU5YAIujM3vPDV8FCU5Mgv+xsk2xm0D6NHkgkXrf3JeOkHUJNCNiot5oK++qC

数组

HRbEXSPStT1Xtds8Bon6AYf1ivFwdz6bpRPjtyTUcs/bKI+ITqpoap52rpGlUtxWd6szrqqWUc7vv2WpgkFJsIXN/I5PRwNHoChgcvk8va90qGaJPXN/Nov7jotiPjiyhANoxqZspRIevpVTBEIKab0uxl/jsxk+csNh/1W1kDLnHO+vE3D+lwxuBwzgmQ/ObpxXfwrgLolQW0dW1SlaWgpPWLU6U/XG3cRgjU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rxTuRvCTdMEEWm7mylDQUj4=

let obj: any = { x: 0 };
// None of the following lines of code will throw compiler errors.
// Using `any` disables all further type checking, and it is assumed 
// you know the environment better than TypeScript.
obj.foo();
obj();
obj.bar = 100;
obj = "hello";
const n: number = obj;

7o3QhrIyq97Wc5lGrYCFt+RorRuJcxlXyrVaZKVEdytNkR1a94n4tyspUt3IuTlLjpazU77rIjPGr9Ae1lp6+2n8I4Of7deWYwlOTjptonw+YqRgFhhJF4pQleovxHtnZesxqHr0kP3HA5QVrdOo0upZvzl03XB+mhz02TvDcbeNUq1FNCmhJf1WuuSNnBaK

noImplicitAny

2niIzvZtvHPACx4HmeXlv7CdVmKnFNJ3jEfEtWSRC6rQnaoeeTPq2B1eWaM0Ps8TSXZroO4HVHq37URwy+hPvSSXZuD9c9ih+YAODSrg//zFiOZ049KEht9dJcRXRKVidJddAovLSVZ5fUuee1SWDzceChxuekhYiGdCa9ULl5Q=

JQQKSEc5wIlyeUSjmUNMxlqLzZAhgktRl18Hf9LkWpTB4G6EKdEeYlA4DdW+3hLcUp+KEnAZ3I9EDmIHW5KLgXvrBeqaK69nuwfXgjP04b0RlPC9K+aL0JI/YUGmqDl7pJJbuomsBLmyXPg2rNDX19VYLgW3H3b9awKJfBo+J1SiPf1eLKH0DkiNHTkifcOqVhS9kgKMap9M0auRKqoI2fsVCI+RYFVPZEBf2wyaIdwxvHD9QLO2jWKPbhmUbvCKhjC4M6d26A4L46GMtYUXgjRDVHSx5NG6SZStTYe9lC0sqovaB/OOdQf8CNgUhHtb5j09XUBp2zis+44MCAPvWA==

变量的类型注释

dCZDmCF4DT3NsmOgoaSjbsH0kebjE4IARm2Sk69TuNSvVCKWkeLwHVizmIdepfgIaipU9foCKwjYi8GRcIxR4DYgiLZZPhJVwpYnUsJnYzXIaQ/31VYej2LQham0bzP7vWztmKc5lxHCdW5Ii93CldRE+IItC8NsztX6MHJ/FjchJn8CBMFy5MhuHIzlsy/vdXV//p+B6bsB5aoYJXLAeA==

let myName: string = "Alice";
//        ^^^^^^^^ Type annotation

QUyCkrDfT76HAZfv7Dox7p/7t1lQKsRsM0o1ysjq2US+4t5Kw4xrN4Zumf6T2vXKpRdkTSsoIdhak5LLgqC1opZok1GQ4xZ3zvao5/Ky3mJ29N2iBzJr6+OJO+epvEDJYjX4Hl4GH4jHdVoEo6iNkI7MuvzI5FISUJM7h2TjegdcQTexllZs5gBzNBXqA+yR

yCmnlqjUFgeRJifMWSeXCH9HjCoHOgNfYbnWCO1hJu2WHmmwYg6hsVZljz+33vAK59Bv95OTLeNOyBMwgA5ov52pyn83DG2bhdJINowDnDShow3jyywXchzwd6wNwfKzs0gLB/U+ZoSWKfJ/4qGTiYdbdSoe2sNro9n33kjf14OhL0VwW177no9/m9yS0TKbHJdSvnDUtze4FsoXUTUTlS8XBLO+NaazceMbbbQWjg4f/2toSyIeH4kpnhbENlmzojq1yWGCVASR5NNK2Pwp+w==

// No type annotation needed -- 'myName' inferred as type 'string'
let myName = "Alice";

tv6Vs5lkzpgl+lD7Loz7q8lOWgcTuAprdABx5BxjtvfpGG1VPR4KePNWR3XU8akZ0mAqna3WGgX9qTAc+I4R9hsDrY8ixVFDnI3W0dDaqMlsG7K6Cw4K5xVaBgEdjoX0NZiC8hcFo8BADg0BxIqEBrnaG9bX35jLdcM4BSVEJs5udwtysukBzDpXE2Q9gHrTcD8KocMSsLVh7vGJGzThrjhseqtQulqeDCilNz/DZzKtQzwgVdd7j1UJLvq7pX6RGOydv+Duwj4EKpeIKpmFsTnxmgq2SiSIKa3cFoYTbuk=

函数

wLUwB9JDfMx9KVyOPSkdXXssrsNXKJU1fCU0opMD3VFDvxGQ64kI9RS72OjKVYOv7H8uEv6wo6BfIcy5RpKuqn/QLLV3e1Lh75JbtNzQSzppWQ2Gnmk+Q94ZLIWYsv4xtOVzr60lD7g8t/fjB+57ploo79p6uZg9uXlJp+lDJPg=

参数类型注解

mQUYHdsC2kZIh67lA6eDu5Y6uMaTU6U+2G8OOkjZkOZWdnP4/xCQU9sJ1oaIBPIVUwSkfc/WTuTTOY8sR09sD7tMBA+ZhLGTstbch8UNc8sHHFXR9uSJ8LFnvx38WzTDaXMEAShvlT05TqcQjrE+ad/Jk/qe2dPpzjK3eblGmf7icqozroPN30kFujXhqI8cL2FmmtDSL3xiZ9RIMwR5XA==

// Parameter type annotation
function greet(name: string) {
  //                 ^^^^^^^^
  console.log("Hello, " + name.toUpperCase() + "!!");
}

EhGCpvz0Gh9RmO8MZCBjv64fUau26OLnLBNJqS3+dfMmR0kRs5YFExA+ccJMXDCAb3hSZwXLINnMMFTNxisaPA==

declare function greet(name: string): void;

// Would be a runtime error if executed!
greet(42);

eGGchMy24wviqSPnHgM90c8HbETq0vPk/HeffUWNCOspGUdT05MFHmgELqPVCVjjRIaIr4oI6KcN1wpRdZksnnfI1Elil2omNKtpdzJ1xQewQU3hPwQprlIAQat43usUlocsULsCHBj4mj83LLu4Ow==

返回类型注解

dJ5GnWwWr6/hAkJKIjj2otCzTtUqhwnXz5GAeGgJ8PI8uv/DTHEHbUJGEnnIABIsSnlL0KnDZgAQw/+F/yMzfkjUP3T1mITv9ecqPhf7owx1tlX8FOy8xmAwNJAHXKsq

function getFavoriteNumber(): number {
  //                        ^^^^^^^^
  return 26;
}

ELnjz3mP2GXSbgJYrQwPwYMWbcN/fQJGazLXZdHEWrKpMnobZS3IM9KZORRA6odyi1plcJH1M+3C1Gdc/sELE27FpO90r6TcXT0DYMIN+6Ia9IduTyveIRSwJ+FiXR9FUN6rRqB2U5RlF2Rq579BSmhxa7cVEKA8Nn0iXq9UxjD1lu2+4JNMDc3eoC4Rks6ARxTeAEBdgC1Ue2joooRHP+1OZO+9/qYnABX/snrIUOZZ0wUKYujsUjEdKOMwkd8agDWs8p7dpxwTxnMbxwL3BIDdXDfusx74P6En3OGWUR1ZR2bFpz9/kkjwoyga4ocvmhfgk3s9rQ8cUlqFhlzboVj3rfmDa95MRwAI5PcG76agCMS48ZIy2DztbZ1DuiVtO4GLHdYMIe+VgMKgUG9ES1gw7bOTZ/Mz38OwGcBMJ36aaLFf50ED47eArzNb/SOSxN3fktmSQJM9b3CUOfaT9g==

匿名函数

UNjXo9uwSvfQwnimP+ftwpng/S64jdP9lnzvRzpP6idKyyTTpqLeglYrz9oEOYnbG3+Rzayruo4vHrOn7s/Oyo1XRVVrXjlsY3NxfNdIgvTQKwNVIEBi8BrKXZratDcIpe/B+8OV68e8MMYEvdljVFz2/UbgLGkawK3ncEU0YheLRM2zOHXnAgKMdGNAfnWyhKt/CT9gsTmmgy58vWIUi1ZvwYZV6mtfdJbWvJYdLuE=

pTILAaeeLUg4Ob6RegYTD1Ekd04lezdl5GJZaeR+RCU=

// No type annotations here, but TypeScript can spot the bug
const names = ["Alice", "Bob", "Eve"];

// Contextual typing for function
names.forEach(function (s) {
  console.log(s.toUppercase());
});

// Contextual typing also applies to arrow functions
names.forEach((s) => {
  console.log(s.toUppercase());
});

E8f6sF2HN6kM6hnlSOBm+/BXDgUcEcw1gB9klSmvYX6htAQNHjLXBWgjjDpwYtDpf7y2dunfnaQuc6jaqiv4EOIFD6nIB0QsRV9G4zHqufsFdsbewPUAd1eOTxayW7b7ddhczM7oz1p7uODAIonOiovnabs31v2j74NYCvqyAt9pppLKAYWiUDqdPGTyjcNy3lXLIliqtQxK1Gkx+8c31PlX/7zKEFPhYIlcLLpStMtIMtRpE4YFd/+UxR1YRISC

07j6lZ3CnL4kVDOIBy5pu9XS66INcxNjn40Uu120D2xfFmhST9lETRTMqud3ZW40UskHCvrAX21pkORX1qRejwas2Uo/r4g3xehnP8IZW1EbAJFr5RK3kHxCabQ2cu9FLoUMm69m4asBHkbMv0/vkg==

xKUOHCNxVMcYZNWXRTzrFpDTEKS66QmnEKL9vb3Otr3ibtrzY8yPH8IY6cBrhXDKhUVe7PNjydL5BG7NGwo4QzX7cR6FvikVmFbq0oOMAUpE+rJoInr1B+eBBB5idILvglmEKnS8mK/Oxake/lhK6Dn+24nkL9P+3eUfQHODUXw5OSD/d38HIfBN6ZMGuUYv5wLKAwiR0fWEk6DUGYPuefPLFxQPVYYpY4/ir5Kkr9qrkD095YKJ99jE64HQNAeQ0f2tyMrf1CRyRXTRqHrONECji+zeScXdDIicgPhaC39LJMy9qd2F9yzfo/jqLYOnx9YvDBANDV8+m8r5nvKgvg==

对象类型

AlccgSpinDSWP6jNLE9u4Wxwlj0aVOahFkPt2tY43QaL8eQTfhpDDP/0KBC5V+mIiRWtLnmfSn3lOiiDHymv9XE3UYs8gK1b3y0bCFmSPhDZrqSoiMC+eWyZErlLwSzrVvpclOjud/zDkN/g0pOldmhNODUzNlRyGqeuweBqeQLDEnHRGwKhn/BbWlXW6nu2Xk9NENCgzHK4qj6OJQzDhKUeV7pxWDSu4n60THx17rfhZjvYcqT8iR3saE9HVhbt51PokR0uy7EmBD/wqY1LD8TDjliXR/J2Jd1KdFtgAUE=

4oY7pWJPR/5JeUi8RAXxURMStfqCN3vaJMjiuVtvyC7M81f6N0H5FrYX0mavFBJ5/+TRsWlEKEfqDNuDtGtFUA==

// The parameter's type annotation is an object type
function printCoord(pt: { x: number; y: number }) {
  //                      ^^^^^^^^^^^^^^^^^^^^^^^^
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}
printCoord({ x: 3, y: 7 });

X8n3mrBoNNYyeU7zHle9P9T3LZUnd2ny+CY5D6r+x6f2npf1IlqbEM4wIo/Gd+A9oNtvEJMF/YmzYKu5u2/Y3+ShBV03Bf3XtxxNPVj/VNTZ05Gpo4rIP/0z2QL3f1y8F6oMYyLHdV027U9KX2WLurCsmQt5/OPHhffHkPiuoMwvuiNL0LC/g/F+MZ2pedyDRmHQDDcAgc9IhJPaerJyKQ7ajadQZx+upUxqXtVBLY5yB3QQilpOFUWrk4yyWywGNaZHiCxVtqAMFU9MBml4kOilakXx7qPCvm6f2GeSJhWm54NZl8f6fitpOPdlKhYSEVdLwYvdrjRWI9rLZfZqF2JGtfuF99AoqFhl040TsfI=

9gJ3Goo0N650ICSnLp5JMYcyUrhtEKqaJIjxW2lzCU0zrJ41eqpvofjseiboszpjuQsLnqwCHrtryEoAfAiD+xZF96EstGNI8WeOQM5Ie2BeRKNpqdUnNJxpq0yCS/yIeVlvlHEQfVEq0HIoEaEgXA==

可选属性

Mlga8x2pf4dDTZqp0BrhFWD8N3ovv1SaxHVlAldGhDQE1BSuX4/+jWk8f0zkEDp8N9bRJ59iybteiySk4lX1pKLkW709o1whsuW/dE2PprA3bkEMFGFgf3RGZ5/Sj+wJHZsNyFC/gbC3Sa+av87aBsWuJGT8VyftGfGNFF4Xga0=

function printName(obj: { first: string; last?: string }) {
  // ...
}
// Both OK
printName({ first: "Bob" });
printName({ first: "Alice", last: "Alisson" });

QH3y4YtEZU9ehN7JBTwc6JsbRmMj6zEIpsOvPLin6f8rG3XPYBsDda+8l2faVTX+8bMLtZTkSH/GZvAgExonILZWEH6yY1Kcw6TYRwNJ/0G896opO+L7ptsmA3+FucEqdAgPtsgs59yWVFLB4podUusX1Si1dP0qOz0pF4vArQx5e+exyT8aF2FDBPpi7X7Yz0DeQfvVYRhfVuOxCBUEEp5g/WrTqvwWIIYsBbsgo+1eiCEL2CcR6PT9km7dS0oMVkmCKUbRHtSjVbK0o5SbRAKAq6ddnJ0JvE8XaY1FAlVPyMCdyj8I6Bt5XBWc8ymociOqgqp9chg2iTDaY1oXPw==

function printName(obj: { first: string; last?: string }) {
  // Error - might crash if 'obj.last' wasn't provided!
  console.log(obj.last.toUpperCase());
  if (obj.last !== undefined) {
    // OK
    console.log(obj.last.toUpperCase());
  }

  // A safe alternative using modern JavaScript syntax:
  console.log(obj.last?.toUpperCase());
}

联合类型

JvBJaEHfmbEdcJsmDvpxQQBRwYlbtUZrslB4Y4aE2F389aWOw4MlU9Jf5SIJjZkOsTsGk3NwCvDe/HvzaWOzPoR8ZWOstUjE+tr02oqUNYVNjpw1hxc1+STQPJUuW8UcMpPRXcFFt+NT8dgsmtFe4ljEfRig2PdXCS6FuM9sBqViLscwSX+TatpYtGHGKppGTAnRDn71tQ8mYXJkRJZTJriJrCCNb2fpZ5vszzaQKKpaqfip/9XxvSAVFHvbMGjQ

定义联合类型

Pzecv8vHwKOOwcml2DzGelcSf3VlweGY4YxnepiQZFFo11xmlOL1EpkUWmtkxX0HoqMeYTsB8puljqYYCV6jgxnA5czc3nSRkuRfskBdF5yOzaRJefcjiz07hq04ye0jS2vjUv6+qVuSOU2k65SJyrTlQR5vx2waLwGXeOpQ4GpwQWVh2CufJfFJOlBvNYENy1UlElr2jF6gNzRAHSAKFgPM1OKWL0V/zTnz2Oa1iKF84f9vZeucJj69BAuOsqTaVjbCyPng6uAvPW0X6Nyh9VmbBoJsYH8GeF+dt8DUI807PknYTCS7+GqupKDUu/uqDL1NToUHgYlJbnCj1jzrmg==

a9bZUVJtlQHrn4tkmmgqUb1tSMlcpp7+JTUknn7p/CgJWaSwCLspAG80uGuwbKlie/Y6i9u5mgFUefvzOcDRBvsvUyoJPDCBKYgyDr2eG1s=

function printId(id: number | string) {
  console.log("Your ID is: " + id);
}
// OK
printId(101);
// OK
printId("202");
// Error
printId({ myID: 22342 });

使用联合类型

wyaavQiEnYpgjshUBUor4MzXHudZ0/Fa1Ka6oh36WQt4X7fygOcFunxOoF97Tetx9hUTpj/XgGVscnx3wHjGKpVubRWD/sG/wGCgWr9bnBeOvTcmc+zUNxm9UNJx5ZefJA8TmzjU/45aJu6wP80YlJ0M41gXiOp/n9LnaHpj5b7twxbM5ey480XOcBWfwuO8pqawMvjVm3ldL0ETRexA64bfhU8g0CmCmqftIRjegvk=

QTth7YYyt++XgHmCsxq27j8j9PoXu0yslCwgoKtvBItjbcdoCWJ7nOxQIOjXbNIiMZPZYgXXDxYuas+C3fI485uH5aQ1C4QPdG+Rb4kbhn6PekJ9Dddi78dK7FM7DCKlbTEghyy0aTeq0IocdwUmjwiPn/R0xStgQo7IzC+xmJ1CetmV+mZv9Np0a6OeUGcPCbiiHuAvqYpVI7nrFL3TothRBMOZAUFGhYYL20fuIMIuiqGNtKy1zKGiTBUIHESBaD4GpVO21u6pDmQN5efi9w==

function printId(id: number | string) {
  console.log(id.toUpperCase());
}

fLvqcDLi/4zkZIhzbyMLGPNsIMkSkUKk8gk3yfukkSBeGFGV51i3imNXQckL9xR5X7wkgGNMK1Exn4zHTxP2jjPnZDxeLfgsNztMCRqqlXKompIeJmYWczNvWdaCi3gc1+d0+2iqG5c5ZTfP2bLDtkwSQ3rDXCR6h6vrx0QE9Fus+FjcF07SfeRvb4nvtyx7BAP00TzOncuaYMLnS5PNiFJWAOoWAMG4SNqmckMnRNj0sfqDeBvQrE3BzQT5O13An3YBjQ+brab6S2Yfgyi5ZA==

Fw+dSIewLb6dyCVlvdzxjXPQOLdFPKAw82RMv7xOSkLbSZtsU4pTkX7B8pPYn3ceFsZJBcBQ69xuXDusoparSt3tj0qdSxUVXnnB/1bvXbaEunRtfUmkZqzRze1urNiLdhSHKGu1qnKWLMAJuo52saTj0kxhSAKCeBmqWjMUMBE=

function printId(id: number | string) {
  if (typeof id === "string") {
    // In this branch, id is of type 'string'
    console.log(id.toUpperCase());
  } else {
    // Here, id is of type 'number'
    console.log(id);
  }
}

rBpRUGVqcWR9ZHP24yJaAOwzLaCy/Dvz3/ZBsFTO8kKqsHt+Vic6DJinjw9pv/JuPy61APLWdE1mKO/Sbn7xWB4e7Sost99fACGx67jCvLY=

function welcomePeople(x: string[] | string) {
  if (Array.isArray(x)) {
    // Here: 'x' is 'string[]'
    console.log("Hello, " + x.join(" and "));
  } else {
    // Here: 'x' is 'string'
    console.log("Welcome lone traveler " + x);
  }
}

ZPujCbiNoIxEOPlrlno6vy9TfZ7VKOowKH2rYvsqUPPzIHdGmvamFyM7qtSi78QYNz8ulipjEa2ObOzAzWdlcXnuaRR+8qncHNJXNB7l+4UR5s5TFSiC4mwcUam9TrKirqLvdM9wDKpBL6VxyTrXbtJk6ao4m5X+871vY2Ou9dPh9AMBcVr3USXwzLgMzx/JLX6mSHM8Ky+HMT3HWpkY3E6N9EUeDW8G5GTttP/iahEgVEpJv02aiPRPkRAwB2+P

zjPLzgHSjN6yfmxpjW3TI9SHQ/3wC6+5UMUAoCdxcTPYkrY0Hs9kQjvz4al6hCZGxh5rphbBePbEQuUpx8nmv+8Pnzakhk3IfsAF0rOj4hISglouPnqsvqPuKLLLjiezY887vsfPkeGXrIN4YlZDDWnoGu1X//igix7iu7poEm6lyagJMVWby0O9oBH2Eu3iladZgLr5IxoALMPVE0+b+TIU/t1WN57jyncFWDCb/6OCh0w8S3xf0tlXOYnMjShOfONW30UGrOLmHQWq7ALA5JDjeY/k3XhN0QEJt3zhyHSNJuuELXwiOWyugLOecZ9MDJTmcVB7cFlzswk63XQDGg==

// Return type is inferred as number[] | string
function getFirstThree(x: number[] | string) {
  return x.slice(0, 3);
}

JTlexlnHQkUs4yNieqtICdmOCReNwaBgr1RIYHi872frCEU9MPq3S9QtpPLJDXBgZ8WGQ53kTWsXG9GKKmJdSc3+AIQQ6sMXxfwDKsHPUBZewBYDFuhnJD3lX9tDUGGZ2XxSj8FtKQLvIdazEDbzkftRtTCayvza40sok3zYL3LzcnI/vuoHWMoWsqAV9LmmLfg7mVwiIXruYmLgXrBav4K4BZprnq9mNW2Vumj1t2Fcn1a8N6dV/08uLD3cTawCRqpIZpdvZkPvwOwUtpcwK3izErEZWmNsRCpmCVn72IsC+JH6nTCO6Mdd1CDA9VL7O1PdhSNK7eELbGObSAEAaMMafU4aKXYdcsB31aIeEeBfWDvJUlvGuOETZ2O6GcyhkWW/STq/ucpfJ2ltdSCEPWU5gtb/tHX6hWzQsCIPuRdKFTOqmJxqTBOcqMxxK709LsZyxDzAasstTAmvirUfVEOTzkI05ZHhkh58sJZk1UmzTxuPzM9UHcMf4DA/0CDrzag2Tv0rhK95SKfGPXURzCBJHBmvzuoQbu2cS/6kxDbgFiXRiX7kQF+Al25R8ntxqU+clGG2bLSR7fL8HPpiVMxvCZO7FSH8BtGGHP/Y5nCedxAJzJRYD91+hWcpTqlb0cMR9Q7xPCo4JOopuAQXAQkDfbzbFl8Rxs/djhvT3OwejNpi736OIPqzyL6Xy3g4i8JWEUuojBPFvSucS9sMRlPpn/R28nztSp3qi29LovyS+a1luqSDaDWN/erkRSyMle8e1iSKSSOwNEHfr1aC5vg8DxZ7Yzgzo7gte6Jkvm0=

类型别名

4HnFEEB8tdhYsXDJawPX5BL3pC+wEfsniBXA5e2TrcwpghxGmY2XNJTKGC+ixP1Lavbdu1E0fdcw0kmYxQx0cp4uFnjEgToYxkk7ygmsRlnpCj758Y/idMtWblJtkhDrIpAW3pklam8qgsUY4HzM4MNtU/RCe5ENXqonJ2dSnzVdYoesOdDTqyIVISS9JZW0mRRh7JPI5lfTh02LUvXuSBvrUG4ntAkkmpsqpB8j1eJjNOG2ZvD/bAgA5x/osTSO

/ktCVmcfaIVYjxXDKKNc6HuBOi0TlyqxlH6t6S/87ALlO0gHYGL1Bv4afnMfTwmQxwrcb8W7GTaZSPfntW5eVZ5cBCnQztu2zBRfMiCw9Yg=

type Point = {
  x: number;
  y: number;
};

// Exactly the same as the earlier example
function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}

printCoord({ x: 100, y: 100 });

YiGKzC2J12ADPlN9fZMiKqJsyWYN74p2D/DffoBKB/bnkuhUI2nFr4s/jQ4FTOMOQRzd1ECRWbNdmUF1JFOHIOv8WUaBF0SCFGCdqhsKCMoWcdD018HIcw5dAhwep/u9P1AtfwR1Z/SmzYB2GdAJzl7QqPb8A7ivlZCBAEVs4Z3QrvR5zcR27h8J2OSCPTf+7tScmPCZFuvpC6CEyOJVSw==

type ID = number | string;

Yl2S/93AGKV7whTZKGd/NiUvoVdlCPGxaeCSSC7zjl32Y+jiE4i4XXDe8TGo8aiQwOhF9fVox13PZ/kwU92KnvS+NdzPgs3jtRoWhMROtkHwFVjqaAKicOuouYX5+TxL2SwfjVTyi0XJzqzKZaON4bCnsOow5UxxGUl74CY6Zd3kfb0w2ZMbObt56la6OAWDql4wqaWNmNPTJU4c6cCdkx7LLwTMFgFBQqUvBwfjbD/kkJoRMA6HNl5BYpyhAz/Jy5XgVv7WbECxGSNxbC9BPX1QYCgbWBVRxyRF5cSyKYEjlwQPblgPaDc4t4m1WGOaG9L1nbWghkg0TLtM+mQZxrjmwj37rTFWoAlZ9AdsWjYe9IKCgHovXlajMIOaZiiyq+XANfzj85OciOS6OFLMouRFq+G/FtGya6sZA00cWG0=

declare function getInput(): string;
declare function sanitize(str: string): string;

type UserInputSanitizedString = string;

function sanitizeInput(str: string): UserInputSanitizedString {
  return sanitize(str);
}

// Create a sanitized input
let userInput = sanitizeInput(getInput());

// Can still be re-assigned with a string though
userInput = "new input";

接口

4bKtU1KCmjA0j2prA2CVuThMqTr8lJmqBZmzJKWihglq8uBvlXiOwXLcEhH8UI9drezZw5R0aT9w/aNhM398jw==

interface Point {
  x: number;
  y: number;
}

function printCoord(pt: Point) {
  console.log("The coordinate's x value is " + pt.x);
  console.log("The coordinate's y value is " + pt.y);
}

printCoord({ x: 100, y: 100 });

X1YjNWhIoegwQScoA6EmzIlPcOEOunL0alhhHn000tLF5jVU5KrEKizpgmMZB6vv2Yp2+bWwQwLiQH0PyiHRZtD1RXeHqJBjWCevi4XooAkxJYNGPZtl4O+wAXce6fYZsA/UM1Ll7D4fniEJdDVJMJcapA40bdkISEy6fKrNmPdz9RrU2L5SXNyldh7iPGIKWwPIwRucvjtmxXcHPUMA2JMHOEjmYjZdORhp43J2aftFuwahRN6ttLIFRoQjvhkixiXK/ER+o7O5UokE3gzhaKlJTwKm+/ppBDSiWm29qqQ7gYgaufbU9Ai8dLf+Yx3s2Uyrz3Dq+hflFFCbmpb3JdIXl5zvVJbBEVj0tEbaYqGs4Xw+uUEpbpzvSUk/PwLpqGqu8FBsKQ8fxhuL7GrymOOg+zSCRsPJD4RPiGVvuKeDc+vAYPSGg6kYpFZN4qNa

类型别名和接口的区别

vAmSe17Q0C94Qm2JC5XmFKtHqrzDS9trkWW7HehE2jBSInInER1yQS6zZvu5vBaX0FiE0V6QiR8M7o10wm3xAobIJQmupeiHyGuk+G53krK0zM8Bv5W03rJuNH5ZqcKc0psphU0vwF3+kR0Q/dstalk076KNtlaXok2OcqPa1MhpXbmBQTSSm36qOxbD9cL5K8l/YfoA4gekXyKi2soQ3qRQBomEbY3cWT9WlfpKZ0AqutwZ+qffFAw+ywuqJaZzmJ6c2Mi7IYVEusXigJlDdOkXZ+V5+uVgTkbHzJFZ7vh+8zqPFViLNxO3hdOgJCDJR7ln8wH4iAnf195POBG8yUW8wf6kYvdgXLeVI8za/Pg=

`Interface``Type`

2he8PIdBmC9ARt/kueLdhw==

interface Animal {
  name: string
}

interface Bear extends Animal {
  honey: boolean
}

const bear = getBear() 
bear.name
bear.honey

esbX4BC4HzzN5vLWZnOlt33DTBrbxfwVs/4/UoGGphU=

type Animal = {
  name: string
}

type Bear = Animal & { 
  honey: boolean 
}

const bear = getBear();
bear.name;
bear.honey;

6lvSMJml5+AGbaIYiG0x+zKd7m5HOkxhMclA6ctTmdU=

interface Window {
  title: string
}

interface Window {
  ts: TypeScriptAPI
}

const src = 'const a = "Hello World"';
window.ts.transpileModule(src, {});

E8TX5uAF8iI4rfhKuo+TnewIrOx6lyU2Ywsx7xTqV/A=

type Window = {
  title: string
}

type Window = {
  ts: TypeScriptAPI
}

 // Error: Duplicate identifier 'Window'.

DCbSvChRR9C5UcWOdIq2zuo7kUUgMeU2CF69W/hSeaK5JOFjgnWFi8o2xW+LYpKKZkEs695W13EaJ11wOywqjWWtnbheIZ4PXwgrctD9/86QZhueJZhkUtfAY0DmOEVDl06mrE8gt5MMfIldhVtAJzSSm7dWvZF3MdVn1csc/ztJe67q7Al/wF0vS6qQUxeR

    iFoM4kgKPN632/aWuAcdp789y5RFO/4dRLo3h3uhUqjgmFu+Gu30yDf4wDGluwpSBp/nVuIrfvjB65cpDdAno16Y+zzUBUcCY4/HaVnLE2QVBMx/60ZNSEXiyzqEIto+ueWhsEWQEb4BGjButvdry4LUXLawO29YG4iCV3J3LI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y1+QQ6BDXUX/aq2g==

类型断言

xmQfc8HB+9HN+TFuFcGDtp0khiPpLGPqk2Y3rGYwLPHMrcY+OlfxLyK1X2He505suDV0APyDmu/rJmp41Emx2M9TgiU4fCP1bsVGSEA66FI=

bWQB+mbS8RBosTCW1jRDqJgCNtWS/wT4Ki/Eomisc98D+vWFquHnApmoPTGiU0GaZZ4H9tdEvzwsM4UUE4bDVgrMoUYI66iQB0uZETl2fOQn1tBew7frJpfTNmrb4N+fH+XUUc8HhZkne/juYykt9HHYYzW+JxUALATqyEfY+UGzxQ4LtwuJ7y2KCXNFwlx/sQO/kv4qLjFpEa9iAnr8AShXk2htzmreVBXh/3X7Wm1gCKJPAE42KnsgYwAEnJvtsznxXR9jT2cRJHUxPh8BqsjVI6Mym0hABeckqr3hS9YMyf4MrBUqXeIT9j7L4gow

1JPwsdvoQHs50xydtrIit7cIr+0ysuUdeksMg0mT7SxWdZX4Q6iKZdFf67ySy2YvB59cfgAWGnwBWWT5q8adhc98p6xEQ6M0cX8eQznjAlw=

const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;

fxmCBGrtQp/wWwjdlhHFkKDnEe3i5G87AfG/6T5MftdEFJstOdBOVJavpxWQT3yVM0O+nkgPVB0wYnin51lNLf075eFPzQt7i4yvmoi5yPC33IdFSwY+g58MLBvKjLp00vg8+x6bs+rlIq4V3NyCHw==

AIfjiSSzEZoTTvVJMvAzdfxGtBZWXXTAtm/M5vLLBmg9kARcSGiIYggclnq6p/qTzfjqT9PuH2dn776wPIIzOA0SEUE/KsUVJYUn4ZWDGmU5klHUHQNR+saQu8x6wjv9W9XDFscOlMTi7hDVWT58og==

const myCanvas = <HTMLCanvasElement>document.getElementById("main_canvas");

583B55boYVLWIM6mLaYUgofL3l/z8yJi54geEgUwaZV6ghmNX4AExpXkAVW4eb2przFDGuwNccGhyards4Au3cvPoRqItWwWEAV1HgXkzgP2vBCr2wRs2W+SaZZsHd/7s9cFb9ktjG/uzP4EBLXpqoAjDHuARpuzhtVJrK3hOavpLIvIV3v1iq66a4V2QHemVn9ks3gaorJ/IQvBw3v2SFXnasLn6BGl5mHw9DH58qZZGKmZQ0yFNTYwmQEm4rYw

02bFGHQ8FhXAs2k9sSJxOHw0ILqkqq7weaMspbMtKgyHozu0Pb0W/uWUhzG4hHSIYuXE+kqsvyO0sQnBsUgBuTbhrJj69xyQhnzOUBIErEnh+Qe5L8DjK1iVrdKJIMYQw6Ge+rbyI+lKSdQbPJI8u+jd6seUW3UeDxFEApQPJYm0oSzBcNXYjfEgeBBM4vCw

const x = "hello" as number;

9OkdUwRKmo4To38K6C9J/p3Dn96+TO2FP6ZgxyWtm+CSqW0GrHB9Dr21meH5YG5EXqZFM2tGXvZBLOMhmXca2/7zvySCju3+H7MPpR6Wea2YJek4lzjV2BNYnu9vPNqIZiBgM1NqsoWeJ3FgnNEhX2RKYO1kckfSVDjwP3OwYXNj1e435+2A2mvHm9JB4JOjUW9qhPaqgi/PFArm+MTny7gR9jsPfj8ZsQ/tezBbgBC6AKXNAT4mQDNM3Val8zjloAiXHAq0P0ofK8NmugtFaC1fpSa7Ce1cDVxueCGAM637rQMMJkfFiUv8D45Vq1Uvzi3c2qbRwptQb58PzuJdJH/HXP/Qnr4TOJJF889hIt4=

declare const expr: any;
type T = { a: 1; b: 2; c: 3 };

const a = (expr as any) as T;

字面类型

QMq2s7qSnFOmizJBSqbuFx22hZQtg2Lnkj1YnI1wJrgjznWfOY+174p3HZDO+t2vRLkpMwxP5DSmnZ04CWZ64lJdaZUYi4TV4TRVi9mpPPAHrT2s9mZol+os3tv/iFIrkLtBI9h6h3oV64hvFoCXLCdJ+EihlwkJhBKKxxADZi4sJ7Zu1RBmTISy/qrs04YJ

11kpE/drfWbsqE56B9A/qZJB51rq297ChFD64vREEriG6lKsM+Wm/GsPYM1D1cXjl/TEYycR+HFSf5oag17r43n7vECNJtcQ+xiXnhnoV74pdkA7uHqs1kAcT8YM4XLJGDHY/XAJb3USvB/IeKufQGEVCpP3y3ex75knll3SHmgqYgYHG0WUq5wZiJTmdBOPY3m7Iy+98Ly8vy2cpOpDAuh4jexnnHMzonrAX5UD+VdgsNpYwJDKV9/hNLN9LbMIWCRPTA60hXsTWmN3BkDnuMnJqC6FOHU1Hb6XTtGxBq4/HNExeUe3XF8iAGuZZXcnu5qdI4niu0q+fl4DxVfXI9afQ6uIYG+DCXWBv5smlHk=

let changingString = "Hello World";
changingString = "Olá Mundo";
// Because `changingString` can represent any possible string, that
// is how TypeScript describes it in the type system
changingString;
// ^?

const constantString = "Hello World";
// Because `constantString` can only represent 1 possible string, it
// has a literal type representation
constantString;
// ^?

vdOlEy1U4xrDyaKsJrGGLh2AJCs68+XM46qQjfk8FiaB7Z7fItkAEkwreufp0IqVclLc0e9526DKrTrKjtQXzA==

let x: "hello" = "hello";
// OK
x = "hello";
// ...
x = "howdy";

Io2OW9M1ufsm67JxR9EQLKyH2ijUlEebk6/QRQI8cD1I5BBoT3lMwM1YXUkyomDlKlyUuvSU6QSwPRriXZTsQA==

H61belSOdoJuF11Lge5JsrHYhZFl3MhospCijNKmHaROy8/zJfufzw+A/CYB9GNI6pK0/06v9hAVHUdedfjCdWOPqU/hLQ36FKaaZ0NNmOxttcC3SBolYhfyC/dxnSzaOb8rcmVBZKvP9oMabxDLLv8BebGh2uovX+lcpdeGIyOglrDQmmb8IxeySc/7O7t/

function printText(s: string, alignment: "left" | "right" | "center") {
  // ...
}
printText("Hello, world", "left");
printText("G'day, mate", "centre");

BsjOCzDhHZESBEo6SBmzaSlqGT1x2R30e8YvjgN/ZRU995pai/TmyIIB9kSgx3Zr

function compare(a: string, b: string): -1 | 0 | 1 {
  return a === b ? 0 : a > b ? 1 : -1;
}

EkmxzKRCZN+r8RZ2851ed/54PmKCIQ/vVLdzaj5xnQFC8OSSYUrlZod24wNQQoTlpGCIRqPKYr8+B9X8UTJN8w==

interface Options {
  width: number;
}
function configure(x: Options | "auto") {
  // ...
}
configure({ width: 100 });
configure("auto");
configure("automatic");

GvtJetLCrxIZlkgt5BRR25OJmMHiFmUtex7MbpQ/PFroHnFJD4dyq6/tsmUN+UtB6NC8LelQ9krbYdeOBJQS1CRAyKjRHjMPwmHa7yqkXKYnFYGD9B6Zc4m3/ok1iOLI0Y8OGZul5tyG9xpXcz96o8JJtv5uGWfXuzeC0H3eYg45P5i/AvlJer3IRNVlHxpOLvRVbnbRQBf4LKgSxqcWB3BFDECss3FTtxPc5Y5604a68saWv3TKazhdIRS65P6gk33qd/rT2VvB0W6H76yTmT3C9I+mlmYUks8TkThFOx/9sM0KDUrv2/EWBADnjVWw0aZd0clQsm3TfCgm6BVM0usAWDSh19KCPH8Cb6LMCLQ=

字面推理

6do98cpy+IoH6mkPAWZ2Jj4NZ08sGQbDSw+9Zx8/VQGbMMZAIPsD1nRyg5jfnbTgSEhqVS6Pf61RYK4ATAWv87JNgBhszlTlprgOncuZGYIzHoOJnDe+fyUxt9VDVZWsjCnD6JfGP0QqN0jARNGCJPRdKwb1D7xcS3HVDH3sDJV3IFOTH8+Ea/Ph/K4C0VP4Pgxe2Xes1c3zmAms1wl80A==

declare const someCondition: boolean;

const obj = { counter: 0 };
if (someCondition) {
  obj.counter = 1;
}

CIqC8fP5BVDUGUODJk83oFTce6J9DRBjM8mok8jtDsELMXo8rzn9ppqTDA7PeR1QE+ag/OJPcsraw8Vrmbnl2g9E48DNjU11kCy9khBU54F4YwZl4U1yEP6Bsehm2B4bqwcuvu1K+iEnYPfkCU7+A64aG3iofqBLFzZkCuuLt8fo1E0bWaPUiCmfd5nOl96llv+U9+Tu7l3bE7wMOQXJ1gPoeuKNdVHvm9YGnHuTKOw0VkA31TeDXPiWnf/VJ3mfdFhFvJnLa9Rc7tJ7ZbKc5TKhmi+XzpVw2nvGfFQQueuQqRTKzV3inQJjhHcnNzcpK2wrh+UpU0FnGDYM2Jvleoog6efMFLbr2V85/lhYB/0=

bXWeUvJzE7tm79GvKjp2XTygMpcylfXDUEnHKNDPd1g=

declare function handleRequest(url: string, method: "GET" | "POST"): void;

const req = { url: "https://example.com", method: "GET" };
handleRequest(req.url, req.method);

spQvUMy3r5BisYXwPtJDx4xE8OaCL4HwdbXZlSjcQdqDolah1Sq3slExQRMFLGSv+RijRhXCYP03mPVYVQgE1DJYTXvjfUkXjejYevtC3FnNh164wmx38jGAVmoqFlOkfDmVH+nKKdMD+W4aW9/Xu3V4gFL53jAGsKuGrymltbSowtQq0XWfaujE5iSyjKIYzMemBgYnEd/ozNGGpJ4xdW7tY4+r4B9qTBy/TfsBssXfBW5pqNVUNQ5jtTjLK8vZb/fAo9BdhTKkRXHqkkfn7H1dJsZumGApDvssSHGUrxOS2vnFzLV8wQGCG0DZVXjNwbtFFRtk6QiQsHKD8Zpe6fmhj4rl6k0gNiU5qKhnmoQuSa1lA2gTG7B8t6jPMXauxOaCgIBzm4HahXd6ULHvC5ShdJ+vUE58ITO3gPAVjzGV4y1TUrN6FvKSxVvf0kZxIaafKNQf6AsM7cQj52GrDv28eJ0bSpLZp4uYRLNbahA=

0fVym6sBqHorMTR9ir1FbbJi/gZZx/jsdr4tsZN1C6iTMH8dkJSNben9Mch/f15P

  1. /xiS+8r4uy1jfm3P4L4ptWHlWFDP7O6MXOQlwCBcOzZnH4bUS+rEL0yvhxyoNlyGChzURt3uRtWb75WuaVii/Jk9rpgzcuIn3T9IxTsYR7k=

    declare function handleRequest(url: string, method: "GET" | "POST"): void;
    
    // Change 1:
    const req = { url: "https://example.com", method: "GET" as "GET" };
    // Change 2
    handleRequest(req.url, req.method as "GET");
    

    6Z3HwPEJXcR8z46fp82DMIfUk9ulg/tyURWKBPTyFC1VA5pm/6vNT/mMvAFXGw+jmhSZBga0PotVqul2MrFLhiD/g7drpTxRffqVYcWqPmXETdzq1ilaJKfCWgyuP1gIGIxoRS/axDNFwajPjjKtFNTM10RvlLYEGlfVOoOu50VWQkC8lo5HeNjhrLAQNWH7QICN38FQS0/1+PO35c6YOvyZcsPpjHNFVkpdqV9Jl2F72eac2JMjsvle+FT/a227A4iOatSv3G9Y5dpH3pbFFmv//tLhbgYBctNJcHqD1MRUUjkJpRLOUevv6iGhN5eflzD38z0WTHafH95MB/HZT1M74e6dv1oztF+c5+Q04ZbuvhrCnaf/uvLT574nK8bwwLmPjOxNLYTmoDPVlgRs8Q==

  2. vPvpj8LIwFH1Z0EuHILm6/TTfkMQb5HWQbu0hVgy4qBQN/+yoXG3u6JoAJR0tsx1ew3Hg9JRmpKStGFQ9foU5L9CJzO8RdYq5DqwLZcIpKU=

    declare function handleRequest(url: string, method: "GET" | "POST"): void;
    
    const req = { url: "https://example.com", method: "GET" } as const;
    handleRequest(req.url, req.method);
    

xySagBuhzrF5yH/lFgWy3hleFfFt+5NmWPr6RXn9wlNmUAIPXwxWd7asHdcVyNvfGpAau1rvJjZzk57YNXl4v7Y1cApZ36WRGKmm86t40mWjbzk1/C7pjwO7A7wjVEgd5kcfRvOF7eC7YPHZgDoBSDtLuyo7Q9Uz7Wm/8K1UOH08ZhJD/5bxVbuqXLPeJwo57hF0e+Ug+ohJP8nh54sjPdYFj8t2AR8UUGmiLiSK0BnQYOKBQjnNmXyW4+WRhp7Tme73vU6fX3Tyg0zVP9SaKroaumzZin03faioGyQWOwI=

null 和 undefined

SqnQbQZsHTtROg2RFAtB+hsa7aAoE74MIGMbBG4Ut7EERiejxyCJGdbi1zUykbCdpctzgk41BrgASsv8fnjjuXXDh3LoChySjXf+eD8T5TSKnkW/PXi+boGpq5tm0vaMvWAwQbZPxiAqGlPs5OgdUudDv3vske210SEAaECrnWk=

UHkwKhB3PKNqrpZrwKjPrv8sTbKoFRFmiq1xZSivKd7H4iuGZ0nEyWwZl5843ktt+rhadKigUjC40jLbFUT1C1oqwgzsvxETbyGJarKbXsxrpmmwJxBggbt4heiEhOWYA2WQpdN7fESvGIMHBsfRxhiP0g9nhwcuimONB965c86e1rHzlcd3yXC2z76n11QUt2x2RpbNCwKPmBskSxmRpvDteD0eYDdp3+7j+kGxHhDjic1FbV02rhmt0oxzXFM2

strictNullChecks 关闭

S1y7bUl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Cp8HNLjfvWw9zd2ZWEIM+Ckq5qgKyM6Qp3v15TvXb6fRTkP4BR87yb+heWPSF8PpznHd8G50T89/V70BqsWJsv74dWhn6Bx5nYgOhTAVlxKl5N4VRt9hmPGL7C6k62m5IefZ5d44sD/UcZmeRpv2A+dObp9FCEuYsYwYPRar2hre/WbGj4acRg7R0=

function doSomething(x: string | null) {
  if (x === null) {
    // do nothing
  } else {
    console.log("Hello, " + x.toUpperCase());
  }
}

非空断言运算符(后缀 !)

0Dgfwm3jqGP+HVdL1gMxccydvZZEjfWVzNCwStLZI6zMy/atOibf67m9kX3w594IJr4JFizV+qwGrmkT4cFyrlqdm4H1r591q04Su637mCdBs96m81/RljeLs2ZORw3ZWPHfIODE4h92xFl2rKjrsyIkAsEIMGe5oQK9n8+UVdLr5fbkSOlttx3az/FfQsXQuFPM+XfmUVvyeEdQDJ0Zw7Re9cY1jwuQluX1HNnoEqTtv+G143oU3rc16i6up59VYIzscLW4Z6DE2/k06WJdPwsRfTypqlVBnnKJ9AtJfHDi6XzaE9DXSFKnTNr1ISPgG4HySBumAbls+A33bdFS73rkJsA96DPO7NdGn9VZLperEc9OvIKmv/nS1Uy2GR1CZvr0mwn2k03SI4ehLUbtrg==

function liveDangerously(x?: number | null) {
  // No error
  console.log(x!.toFixed());
}

dcunjb9wyjkshRKk8J1Ue68JuyFT2drhgbpLL9xdDUrqCRO1UKODP4g5EPuhGsMXvh30fTda+jx7wiEXNLD6qmIfUGVXCWZCZ8V9JV04Fu9xIj2oLaWVlz/nkkyR7okpgDo7oNSVEEr3jqQdRWj1VVxnaKuFtDMbH4zms5fKa0fBkB7gH1ZIBJtaPVtC2B8s+1rxkD8CN51gB2h+ftEOKuM/S3yFadg0KleR+XZo/agYlkKIe+b4BLDpBpqn65fHgL0x06nxyeoDVzktkz0UyQ==

枚举

AoIVl/7BBbm+j4hYDJj9ToQ6v4xMc9pMsOZShPfjfArUFRQ853ogx8DViLOCURQTYVZ45Trj/aAcwvFznj+ZL3SUHzv23wlZzfwC+6lGXaZ/VAKUjOdfGctwRACMLpdevlIjs7kJFAlPBamwQYSGf//L0bfGq5R0JmiX41roL/DyLPEfajg7pPUEkir1ZdeQzjopEWKyEAnf7op8j1r7wLnxA6H+cUKLUCrFFy9MQGmfhuFQLIZySpwYBHXLTsoTi1qJbqCIGF1uRB3oJpXj3GWLBU6xQbGdZbvFsZM324DGStXyxHB8bb4yFc2VC/vsT/Tb2Inj/NdHbbOe10n9ZHqtyvN8Etdb2NC56LptVPUIr8zu0sXzQ2WfLjmi3JXkKUul2BslKcpdPF7zp//ca3wC7brVjjEmQlrMWctcYjBZFL/s+1JCq8jZeLcASCp7xgGP3A85+vNxXLAed7RRe1v4IbVjPphmX1ZiRXoMhidjnUIucx4Qh8dNoZQ5zOYhwrZ+JRct9z1/MU7CqpLGe/ByG+9lyzb9C9/nUDTwLsg0Oo2+gVCw2mIan4uXBPoXfu7LrQ9OPEt/6BH5SILuG4whvx+z2Ex/BNl9prFE9oVdHKj2KM1FRxs3ByR2+3sidqJ34C+k+/jTPfRhs9r6WxffjG5Vlgo6rhl/4IQbf6I=

不太常见的原语

Emb8ZoxCKchuwMwvi/OMTBD8iXEJHWCcyZEcXg9RuyMcaMBEFR8nsOfZV/zAP1uHvwnjufdduhjHCdDQKrQY5tyOib6X9WVaBF65TbaUsdP28KAXhWTfJQzADkyMOceIJk5CMHQcfc90AMQbWGfAqJ99Mu7qOXj3mwuGSucnYN0=

bigint

/6iMVCN1wMRxt1bIpvAliWaxbs6q/hpa50Uax5LpeaC2edwQhrA7Gx82innuP8keRE3uFpvY+2AC1lGE7lcAtQkmX5NGNM92PHuPDVi5ntQ3y3mXa/IAYnBmvUBSBQV/ijAoxLdLZQuQ7+4P6TTv0Q==


// Creating a bigint via the BigInt function
const oneHundred: bigint = BigInt(100);

// Creating a BigInt via the literal syntax
const anotherHundred: bigint = 100n;

clF9UpJgCdtRBK0+C9dDHDnQAhJXtq2o5D4dHeP6loAXUR6ElTNOliifB99J77+pJNdCOyuGCwv7tzh5J2jv58BLemMN/3Vr7w7peMB4vlw5HkQx9TmxMTfA/qB88Dt7BemvGVdJ60Cdnwmra3eA1sgu9jqQY9+nJwx4bX6GIDCw5/K7109n0wOx/Z1H1lLK

symbol

yjPcKjn1HZtKVB9+nvyq+U3encgZgPlBILUqe0pMm1Naq510A3/qZfv5TG/ZhyKAof4/BQDproTes5k/UJc0h4WKjUY3R4BI2STUkgba5taqnTs+C1VCTZHLkL5TmZuOjdTCEEtGbyEyKf/3pIov5A==

const firstName = Symbol("name");
const secondName = Symbol("name");

if (firstName === secondName) {
  // Can't ever happen
}

clF9UpJgCdtRBK0+C9dDHK/lAfj8OKWzbk1aNMUv1H38ZKw3/4j8V6w+ZqgfiF0Fev7bp8CMD9mSvvCdrPDgP4QFPg1DaV5NqAVxCYkYwEJD1uIbwomiw0kem6IpJzRlSMurp3BBUIhysA3RNQiaHjY0R+fEAaC/wZS3uARD9Fw=