{"id":301163,"date":"2020-04-02T15:00:15","date_gmt":"2020-04-02T15:00:15","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=301163"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=301163","title":{"rendered":"Quartet 9: Allegro | TypeScript"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/495332\/\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0430\u0441\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 <a href=\"https:\/\/www.npmjs.com\/package\/quartet\" rel=\"nofollow\"><code>quartet<\/code><\/a> \u0431\u044b\u043b\u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0446\u0435\u043b\u0438-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>TypeScript<\/li>\n<li>\u041a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u044c<\/li>\n<li>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430<\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/post\/494632\/\">\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c <a href=\"http:\/\/tps:\/\/www.npmjs.com\/package\/quartet\" rel=\"nofollow\"><code>quartet<\/code><\/a> \u043d\u0430 TypeScript.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"motivaciya\">\u041c\u043e\u0442\u0438\u0432\u0430\u0446\u0438\u044f<\/h2>\n<p>  <\/p>\n<p>\u041c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043d\u0430 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c TypeScript. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043a\u043e\u0433\u0434\u0430 \u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b \u044d\u0442\u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u043c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u0447\u0435\u043b\u043e\u0432\u0435\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u043d\u0430\u0435\u0442 TypeScript, \u043d\u0435 \u0438\u0437\u0443\u0447\u0430\u043b <a href=\"https:\/\/www.npmjs.com\/package\/quartet\" rel=\"nofollow\">quartet<\/a>, \u043a\u0430\u043a \u043d\u0435\u0447\u0442\u043e \u0435\u043c\u0443 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u043e\u0432\u043e\u0435, \u0430 \u0443\u0437\u043d\u0430\u0432\u0430\u043b \u0432 \u044d\u0442\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 \u0442\u043e, \u0447\u0442\u043e \u043e\u043d \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442.<\/p>\n<p>  <\/p>\n<h2 id=\"user-defined-type-guards\">User-Defined Type Guards<\/h2>\n<p>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u041c\u044b \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 API. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0433\u0430\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0442\u0438\u043f:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">interface User {   id: string;   name: string;   gender: &quot;male&quot; | &quot;female&quot;;   age: number;   phoneBook: {     [name: string]: string;   }; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const probablyUser: unkown = { ... }  if (checkUser(probablyUser)) {     \/\/ probablyUser has type User     console.log(probablyUser.name) } else {     \/\/ probablyUser has type unkown     throw new Error('Probably User has not type User') }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u0446\u0435\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f <a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/advanced-types.html#user-defined-type-guards\" rel=\"nofollow\">User-Defined Type Guards<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u0438\u0434:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">function checkUser(probablyUser: any): probablyUser is User {   \/\/ ... }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>quartet<\/code> \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">import { v } from &quot;quartet&quot;;  const checkUser = v({   id: v.string,   name: v.string,   gender: [&quot;male&quot;, &quot;female&quot;],   age: v.number   phoneBook: {     [v.rest]: v.string,   } });<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u0432 \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f TypeGuard&#8217;\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">chechUser: (value: any) =&gt; boolean;<\/code><\/pre>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0451 TypeGuard&#8217;\u043e\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u0435\u043a\u043b\u0430\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0438\u043f \u0431\u0443\u0434\u0435\u0442 \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u0441\u044f \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439. \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const checkUser = v&lt;User&gt;({   \/\/ ... });<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">chechUser: (value: any) =&gt; value is User<\/code><\/pre>\n<p>  <\/p>\n<p>\u0415\u0441\u0442\u044c \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043a\u0430\u0441\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u0443\u043d\u043a\u0442\u0430:<\/p>\n<p>  <\/p>\n<h3 id=\"garantii\">\u0413\u0430\u0440\u0430\u043d\u0442\u0438\u0438<\/h3>\n<p>  <\/p>\n<p>\u0421\u0430\u043c \u0444\u0430\u043a\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043c\u043e\u0436\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0439 \u0442\u0438\u043f \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u0445\u0435\u043c\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0441\u0442\u043e\u0440\u043e\u0436\u0438\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const checkNumber = v&lt;number&gt;({ name: v.string }); \/\/ checkNumber: (value: any) =&gt; value is number<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0438\u0434\u0438\u043c \u043d\u0435\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u2014 \u0441\u0445\u0435\u043c\u0430 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u0434\u043b\u044f \u0442\u0438\u043f\u0430 <code>{ name: string }<\/code>, \u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0443\u043a\u0430\u0437\u0430\u043b, \u0447\u0442\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0442\u0438\u043f <code>number<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043d\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u043b\u0430 \u0434\u043e\u043f\u0443\u0449\u0435\u043d\u0430 \u043d\u0430\u043c\u0435\u0440\u0435\u043d\u043e. \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u043d\u0435\u0434\u043e\u043f\u0443\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e(\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 <a href=\"https:\/\/habr.com\/ru\/post\/450238\/\">\u0441\u0442\u0430\u0442\u044c\u0435<\/a>) \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0441\u0445\u0435\u043c\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0432\u0441\u0451 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 \u2014 \u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 &quot;\u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u043e\u0439&quot; \u0438 \u043c\u0435\u043d\u0435\u0435 \u043b\u0435\u0433\u043a\u043e\u0439 \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0438 \u0447\u0442\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0435 \u043f\u043e\u0442\u0435\u0440\u044f\u0432 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/b><\/p>\n<div class=\"spoiler_text\">\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0438, \u043a\u043e\u0433\u0434\u0430 \u044f \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u043b \u0442\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>v<\/code>. \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u044f \u043f\u0438\u0441\u0430\u043b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const v: &lt;T&gt;(schema: Schema) =&gt; (value: any) =&gt; value is T;<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0442\u0438\u043f\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c.<\/p>\n<p>  <\/p>\n<p>\u042f \u043d\u0435 \u0445\u043e\u0442\u0435\u043b, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 TypeGuard \u0431\u044b\u043b\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const v: &lt;T = any&gt;(schema: Schema) =&gt; (value: any) =&gt; value is T;<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u043e \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u043b\u043e \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043b\u0430 \u2014 \u043e\u043d\u0430 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0442\u0438\u043f <code>never<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const checkNumber = v(v.number); const value: any = &quot;123&quot;;  if (!checkNumber(value)) {   \/\/ value has type never }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0443 \u044d\u0442\u043e \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043d\u0435 <code>\u043b\u044e\u0431\u043e\u0433\u043e<\/code> \u0442\u0438\u043f\u0430, \u0442\u043e \u043e\u043d\u0430 <code>\u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e<\/code> \u0442\u0438\u043f\u0430.<\/p>\n<p>  <\/p>\n<p>\u041c\u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0438\u043c\u0435\u0442\u044c \u0442\u0438\u043f, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043c\u043e\u0433 \u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 <code>T<\/code> \u0442\u0438\u043f\u043e\u043c <code>any<\/code> \u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0431\u044b \u043e\u0434\u0438\u043d \u0442\u0438\u043f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430, \u0430 \u0435\u0441\u043b\u0438 <code>T === any<\/code>, \u0442\u043e \u0434\u0440\u0443\u0433\u043e\u0439.<\/p>\n<p>  <\/p>\n<p>\u042f \u0445\u043e\u0442\u0435\u043b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const v: &lt;T = any&gt;(   schema: Schema ) =&gt; IfAny&lt;T, (value: any) =&gt; boolean, (value: any) =&gt; value is T&gt;;<\/code><\/pre>\n<p>  <\/p>\n<pre><code class=\"plaintext\">type IfAny&lt;T,A,B&gt; = \/\/ ...<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0442\u0443\u0442 \u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0438\u0441 \u2014 \u043f\u0438\u0441\u0430\u043b \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u043d\u043e \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u0430 \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0438\u0442\u043e\u0433\u0435 \u0442\u0430\u043a\u0430\u044f \u0438\u0434\u0435\u044f:<\/p>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0442\u0438\u043f\u043e\u0432 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0434\u0442\u0438\u043f\u043e\u043c \u0442\u0438\u043f\u0430 T \u2014 \u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u043e\u043c <code>any<\/code><\/p>\n<p>  <\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u0432\u043e\u0442 \u044d\u0442\u043e:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">type IfAny&lt;T, A, B&gt; = true extends T   ? &quot;1&quot; extends T     ? 1 extends T       ? {} extends T         ? (() =&gt; void) extends T           ? null extends T             ? A             : B           : B         : B       : B     : B   : B;<\/code><\/pre>\n<p>  <\/p>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043b, \u0447\u0442\u043e \u043d\u0438\u043a\u0442\u043e \u0432 \u0437\u0434\u0440\u0430\u0432\u043e\u043c \u0443\u043c\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434, \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0438\u043f: <code>boolean | number | string | object | function | null<\/code> \u0438 \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043d\u0435 \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u043b\u0441\u044f \u0431\u044b\u0442\u044c \u0435\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043e\u043c <code>any<\/code>.<\/p>\n<\/div>\n<\/div>\n<p>  <\/p>\n<h2 id=\"shozhest-shemy-i-typescript-tipov\">\u0421\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u0421\u0445\u0435\u043c\u044b \u0438 TypeScript \u0442\u0438\u043f\u043e\u0432<\/h2>\n<p>  <\/p>\n<p>\u042f \u0445\u043e\u0442\u0435\u043b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u0443, \u0438\u043c\u0435\u044f \u0442\u0438\u043f TypeScript&#8217;a \u0437\u0430\u043d\u0438\u043c\u0430\u043b\u043e \u043a\u0430\u043a \u043e\u043c\u0436\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <a href=\"https:\/\/www.npmjs.com\/package\/@hapi\/joi\" rel=\"nofollow\"><code>@hapi\/joi<\/code><\/a> \u0438 <a href=\"https:\/\/www.npmjs.com\/package\/ajv\" rel=\"nofollow\"><code>ajv<\/code><\/a>, \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0442\u0438\u043f\u0430 <a href=\"#user-defined-type-guards\"><code>User<\/code><\/a>.<\/p>\n<p>  <\/p>\n<p>\u0411\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u0430\u0439\u0442\u043e\u043c <a href=\"https:\/\/text-compare.com\/\" rel=\"nofollow\">Text Compare<\/a> \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u044c.<\/p>\n<p>  <\/p>\n<h3 id=\"quartet\">quartet<\/h3>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const checkUser = v({   id: v.string,   name: v.string,   gender: [&quot;male&quot;, &quot;female&quot;],   age: v.number   phoneBook: {     [v.rest]: v.string,   } })<\/code><\/pre>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/91\/4j\/j2\/914jj2cnjennbg7gflmgj6lq1fm.jpeg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043b-\u0432\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432: 24<\/p>\n<p>  <\/p>\n<h3 id=\"hapijoi\"><a href=\"https:\/\/habr.com\/ru\/users\/hapi\/\" class=\"user_link\">hapi<\/a>\/joi<\/h3>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const schema = j.object({   id: j.string().required(),   name: j.string().required(),   gender: j     .string()     .valid(&quot;male&quot;, &quot;female&quot;)     .required(),   age: j.number().required(),   phoneBook: j.object().pattern(\/.*\/, j.string()) });<\/code><\/pre>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/d7\/ph\/o4\/d7pho4re5sqwjjx6fr9xxvyoiwk.jpeg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u0422\u0443\u0442 \u0441\u0430\u0439\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0441\u0432\u0435\u0442\u0438\u043b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043d\u043e \u0435\u0441\u043b\u0438 \u0442\u043e\u0447\u043d\u043e \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0434\u043e \u0431\u0443\u0434\u0435\u0442 118 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>  <\/p>\n<h3 id=\"ajv\">ajv<\/h3>\n<p>  <\/p>\n<pre><code class=\"plaintext\">const checkUser = a.compile({   type: &quot;object&quot;,   required: [&quot;id&quot;, &quot;name&quot;, &quot;gender&quot;, &quot;age&quot;, &quot;phoneBook&quot;],   properties: {     id: { type: &quot;string&quot; },     name: { type: &quot;string&quot; },     gender: { type: &quot;string&quot;, enum: [&quot;male&quot;, &quot;female&quot;] },     phoneBook: {       type: &quot;object&quot;,       additionalProperties: {         type: &quot;string&quot;       }     }   } });<\/code><\/pre>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/g7\/qi\/oj\/g7qioj3rzbf3dxclqrn-5up7gto.jpeg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u041e\u043d\u0438 \u0438 \u043d\u0435 \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u043b\u0438\u0441\u044c \u0431\u044b\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u043c\u0438, \u043d\u043e \u0440\u0430\u0437\u043d\u0438\u0446\u0430 146 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n<p>  <\/p>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/5y\/c8\/fv\/5yc8fvb48ajlp3owgc1ik2hcx9k.jpeg\" alt=\"image\"><\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e \u044d\u0442\u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0435 \u0441\u0442\u0440\u0435\u043c\u0438\u043b\u0438\u0441\u044c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u044b \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043d\u0430 TypeScript. \u041d\u043e, \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434, \u044d\u0442\u043e \u043d\u0435 \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0442\u0430\u043a\u0430\u044f \u0441\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043b\u044e\u0441\u043e\u043c.<\/p>\n<p>  <\/p>\n<h3 id=\"itogo\">\u0418\u0442\u043e\u0433\u043e<\/h3>\n<p>  <\/p>\n<p>\u041d\u0430\u043b\u0438\u0447\u0438\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 TypeGuard \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0430 \u2014 \u043e\u0434\u043d\u0430 \u0438\u0437 \u043c\u0435\u043b\u043e\u0447\u0435\u0439, \u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0445.<\/p>\n<p>  <\/p>\n<p>\u0421\u0445\u043e\u0436\u0435\u0441\u0442\u044c \u0441\u0445\u0435\u043c \u0441 TypeScript \u0442\u0438\u043f\u0430\u043c\u0438 \u0431\u044b\u043b\u0430 \u0441\u0434\u0435\u043b\u0430\u043d\u0430 \u043d\u0430\u0440\u043e\u0447\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439. \u041d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u2014 \u044d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0445\u0435\u043c\u044b \u0431\u043e\u043b\u0435\u0435 \u043b\u0435\u0433\u043a\u0438\u043c\u0438 \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/495332\/\"> https:\/\/habr.com\/ru\/post\/495332\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/495332\/\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b\u0430\u0441\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 <a href=\"https:\/\/www.npmjs.com\/package\/quartet\" rel=\"nofollow\"><code>quartet<\/code><\/a> \u0431\u044b\u043b\u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0446\u0435\u043b\u0438-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>TypeScript<\/li>\n<li>\u041a\u0440\u0430\u0442\u043a\u043e\u0441\u0442\u044c<\/li>\n<li>\u041f\u0440\u043e\u0441\u0442\u043e\u0442\u0430<\/li>\n<li><a href=\"https:\/\/habr.com\/ru\/post\/494632\/\">\u041f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c<\/a><\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c <a href=\"http:\/\/tps:\/\/www.npmjs.com\/package\/quartet\" rel=\"nofollow\"><code>quartet<\/code><\/a> \u043d\u0430 TypeScript.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-301163","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301163"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301163\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}