{"id":361909,"date":"2024-05-21T01:23:37","date_gmt":"2024-05-21T01:23:37","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=361909"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-05-21T01:23:37","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=361909","title":{"rendered":"<span>TypeScript: infer \u0438 conditional types. \u041f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 TS \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a56\/1b9\/cf0\/a561b9cf0817030d23b0160065feb537.png\" width=\"1280\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a56\/1b9\/cf0\/a561b9cf0817030d23b0160065feb537.png\"\/><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u043d\u0434\u0440\u0435\u0439, \u044f Frontend \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a. <\/p>\n<h3>\u041e \u0441\u0442\u0430\u0442\u044c\u0435<\/h3>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 TypeScript. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c conditional types, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438, \u0443\u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u0443\u044e \u0440\u043e\u043b\u044c \u0438\u0433\u0440\u0430\u044e\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430 extends \u0438 infer. <\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u044f\u0437\u044b\u043a\u0430, \u0432 \u044d\u0442\u043e\u043c \u0432\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u0430 \u0438\u0437 \u043c\u043e\u0438\u0445 \u043f\u0440\u043e\u0448\u043b\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439: <br \/><a href=\"https:\/\/habr.com\/ru\/articles\/762902\/\" rel=\"noopener noreferrer nofollow\">TypeScript \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a><\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c. \u041d\u0430\u0432\u044b\u043a\u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432.<\/p>\n<h3>\u041d\u0430\u0432\u0438\u0433\u0430\u0442\u043e\u0440<\/h3>\n<ul>\n<li>\n<p><a href=\"#ct\" rel=\"noopener noreferrer nofollow\">conditional types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#ict\" rel=\"noopener noreferrer nofollow\">Infer in conditional types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#rt\" rel=\"noopener noreferrer nofollow\">ReturnType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#at\" rel=\"noopener noreferrer nofollow\">ArrayType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#fat\" rel=\"noopener noreferrer nofollow\">FirstArgType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#cit\" rel=\"noopener noreferrer nofollow\">CustomInstanceType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#off\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#mat\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f<\/a><\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"ct\" id=\"ct\"><\/a><\/p>\n<h3>Conditional types<\/h3>\n<p><em>Conditional types<\/em> \u0438\u043b\u0438 \u0436\u0435 &#171;\u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b&#187; \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f\u044b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044f. \u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e, \u0442\u043e \u044d\u0442\u043e \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u0439 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0439 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u0430, \u0430 \u043d\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. <\/p>\n<p><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 <\/em><strong><em>true<\/em><\/strong><em> : \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 <\/em><strong><em>false<\/em><\/strong><\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type ConditionalType&lt;T> = T extends TypeA ? TypeA : never  type ResultType1 = ConditionalType&lt;TypeA> \/\/ TypeA type ResultType2 = ConditionalType&lt;TypeB> \/\/ never<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438, \u0430 \u043d\u0435 \u0442\u0438\u043f\u0430\u043c\u0438, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0432 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432. <\/p>\n<p><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0415\u0441\u043b\u0438 <\/em><strong><em>true<\/em><\/strong><em> : <\/em><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0415\u0441\u043b\u0438 <\/em><strong><em>true<\/em><\/strong><em> : \u0415\u0441\u043b\u0438 <\/em><strong><em>false<\/em><\/strong><\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type ConditionalType&lt;T> = T extends TypeA ? TypeA : T extends TypeB ? TypeB : never  type ResultType1 = ConditionalType&lt;TypeA> \/\/ TypeA type ResultType2 = ConditionalType&lt;TypeB> \/\/ TypeB type ResultType3 = ConditionalType&lt;string> \/\/ never<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0441\u043b\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432, \u0441\u043e\u0432\u0435\u0442\u0443\u044e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438 \u0432 \u043f\u0435\u0441\u043e\u0447\u043d\u0438\u0446\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. <\/p>\n<p>\u0412\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <em>extends<\/em>. \u041a\u0430\u043a \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 ? <br \/>Extends \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u043b\u0438 \u0442\u0438\u043f T \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f TypeA, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 T \u0442\u0430\u043a \u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f TypeA. <\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>conditional types<\/em> \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440: <\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type SafeType&lt;T extends TypeA> = T['id']  type ResultType1 = SafeType&lt;TypeA> \/\/ string type ResultType2 = SafeType&lt;TypeB> \/\/ ERROR: Type 'TypeB' does not satisfy the constraint 'TypeA'.<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <em>constraints types<\/em> \u0438\u043b\u0438 \u0436\u0435 <em>&#171;\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0435 \u0442\u0438\u043f\u044b&#187;<\/em>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 typeof \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0441\u0435\u0431\u044f \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043e\u0442 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043d\u0430\u0448\u0438 \u043c\u0435\u0442\u043e\u0434\u044b. <\/p>\n<p><a class=\"anchor\" name=\"ict\" id=\"ict\"><\/a><\/p>\n<h3>Infer in conditional types<\/h3>\n<p>\u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <em>infer<\/em> \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443: <\/p>\n<pre><code>type TypeA = { id: string }  type TypeB = { id: number }  type InferType&lt;T> = T extends { id: infer P } ? P extends string ? string : number : any  type ResultType1 = InferType&lt;TypeA> \/\/ string type ResultType2 = InferType&lt;TypeB> \/\/ number type ResultType3 = InferType&lt;object> \/\/ any<\/code><\/pre>\n<p><a class=\"anchor\" name=\"rt\" id=\"rt\"><\/a><\/p>\n<h4>ReturnType<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<br \/>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u0433\u0434\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 ReturnType: <\/p>\n<pre><code>type CustomReturnType&lt;T> = T extends (...args: any[]) => infer P ? P : any  type ResultType1 = CustomReturnType&lt;() => void> \/\/ void type ResultType3 = CustomReturnType&lt;() => number> \/\/ number<\/code><\/pre>\n<p><em>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435:<\/em><\/p>\n<p><a class=\"anchor\" name=\"at\" id=\"at\"><\/a><\/p>\n<h4>ArrayType<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c union \u0442\u0438\u043f \u0438\u0437 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. <\/p>\n<pre><code>type ArrayType&lt;T> = T extends (infer Item)[] ? Item : T  const arr = [1, '2', null, undefined]  type ResultType = ArrayType&lt;typeof arr> \/\/ string | number | null | undefined<\/code><\/pre>\n<p><a class=\"anchor\" name=\"fat\" id=\"fat\"><\/a><\/p>\n<h4>FirstArgType<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0441\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u0438 \u0441 \u0442\u0440\u0435\u0442\u044c\u0438\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u0438\u043b\u0438 \u0436\u0435 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<pre><code>type CustomType&lt;T> = T extends (id: infer ID, ...args: any[]) => unknown ? ID : never  type ResultType1 = CustomType&lt;(id: string) => void> \/\/ string type ResultType3 = CustomType&lt;(id: number) => void> \/\/ number<\/code><\/pre>\n<p><a class=\"anchor\" name=\"cit\" id=\"cit\"><\/a><\/p>\n<h4>CustomInstanceType<\/h4>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e InstanceType \u0438\u0437 TS. <\/p>\n<pre><code>type CustomInstanceType&lt;T> = T extends new (...args: any[]) => infer P ? P : any  interface ConstructorI {     new (arg: number): string }  type ResultType1 = CustomInstanceType&lt;ConstructorI> \/\/ string type ResultType2 = InstanceType&lt;ConstructorI> \/\/ string<\/code><\/pre>\n<p><a class=\"anchor\" name=\"off\" id=\"off\"><\/a><\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>TypeScript &#8212; \u044d\u0442\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u043c \u0438 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u0438\u0435\u043c\u044b \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 TypeScript. <\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 TypeScript &#8212; \u044d\u0442\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0438 \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0443\u0435\u0442\u0435\u0441\u044c, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u0431\u0443\u0434\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p><a class=\"anchor\" name=\"mat\" id=\"mat\"><\/a><\/p>\n<h3>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f<\/h3>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/macloud\/articles\/559902\/\" rel=\"noopener noreferrer nofollow\">\u041e\u0441\u043d\u043e\u0432\u044b<\/a><br \/><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html\" rel=\"noopener noreferrer nofollow\">Utility Types<\/a><br \/><a href=\"https:\/\/www.typescriptlang.org\/cheatsheets\" rel=\"noopener noreferrer nofollow\">\u0428\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u043f\u043e TS \u0432 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/articles\/762902\/\" rel=\"noopener noreferrer nofollow\">TypeScript \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/778190\/\"> https:\/\/habr.com\/ru\/articles\/778190\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440! \u041c\u0435\u043d\u044f \u0437\u043e\u0432\u0443\u0442 \u0410\u043d\u0434\u0440\u0435\u0439, \u044f Frontend \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a. <\/p>\n<h3>\u041e \u0441\u0442\u0430\u0442\u044c\u0435<\/h3>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0439 TypeScript. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c conditional types, \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438, \u0443\u0437\u043d\u0430\u0435\u043c \u043a\u0430\u043a\u0443\u044e \u0440\u043e\u043b\u044c \u0438\u0433\u0440\u0430\u044e\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430 extends \u0438 infer. <\/p>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438 \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u044f\u0437\u044b\u043a\u0430, \u0432 \u044d\u0442\u043e\u043c \u0432\u0430\u043c \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u0430 \u0438\u0437 \u043c\u043e\u0438\u0445 \u043f\u0440\u043e\u0448\u043b\u044b\u0445 \u0441\u0442\u0430\u0442\u0435\u0439: <br \/><a href=\"https:\/\/habr.com\/ru\/articles\/762902\/\" rel=\"noopener noreferrer nofollow\">TypeScript \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a><\/p>\n<p>\u0421\u0442\u0430\u0442\u044c\u044f \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0430 \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0445\u043e\u0447\u0435\u0442 \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c. \u041d\u0430\u0432\u044b\u043a\u0438 \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0442\u0438\u043f\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u043c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432.<\/p>\n<h3>\u041d\u0430\u0432\u0438\u0433\u0430\u0442\u043e\u0440<\/h3>\n<ul>\n<li>\n<p><a href=\"#ct\" rel=\"noopener noreferrer nofollow\">conditional types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#ict\" rel=\"noopener noreferrer nofollow\">Infer in conditional types<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#rt\" rel=\"noopener noreferrer nofollow\">ReturnType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#at\" rel=\"noopener noreferrer nofollow\">ArrayType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#fat\" rel=\"noopener noreferrer nofollow\">FirstArgType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#cit\" rel=\"noopener noreferrer nofollow\">CustomInstanceType<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#off\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#mat\" rel=\"noopener noreferrer nofollow\">\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f<\/a><\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><a class=\"anchor\" name=\"ct\" id=\"ct\"><\/a><\/p>\n<h3>Conditional types<\/h3>\n<p><em>Conditional types<\/em> \u0438\u043b\u0438 \u0436\u0435 &#171;\u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b&#187; \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0442\u0438\u043f\u044b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044f. \u0415\u0441\u043b\u0438 \u043a\u043e\u0440\u043e\u0442\u043a\u043e, \u0442\u043e \u044d\u0442\u043e \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u0439 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0439 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0439 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u0430, \u0430 \u043d\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438. <\/p>\n<p><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 <\/em><strong><em>true<\/em><\/strong><em> : \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c, \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 <\/em><strong><em>false<\/em><\/strong><\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type ConditionalType&lt;T> = T extends TypeA ? TypeA : never  type ResultType1 = ConditionalType&lt;TypeA> \/\/ TypeA type ResultType2 = ConditionalType&lt;TypeB> \/\/ never<\/code><\/pre>\n<p>\u0422\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438, \u0430 \u043d\u0435 \u0442\u0438\u043f\u0430\u043c\u0438, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u043f\u043e \u0446\u0435\u043f\u043e\u0447\u043a\u0435, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0432 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432. <\/p>\n<p><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0415\u0441\u043b\u0438 <\/em><strong><em>true<\/em><\/strong><em> : <\/em><strong><em>\u0423\u0441\u043b\u043e\u0432\u0438\u0435<\/em><\/strong><em> ? \u0415\u0441\u043b\u0438 <\/em><strong><em>true<\/em><\/strong><em> : \u0415\u0441\u043b\u0438 <\/em><strong><em>false<\/em><\/strong><\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type ConditionalType&lt;T> = T extends TypeA ? TypeA : T extends TypeB ? TypeB : never  type ResultType1 = ConditionalType&lt;TypeA> \/\/ TypeA type ResultType2 = ConditionalType&lt;TypeB> \/\/ TypeB type ResultType3 = ConditionalType&lt;string> \/\/ never<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0441\u043b\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0442\u0438\u043f\u043e\u0432, \u0441\u043e\u0432\u0435\u0442\u0443\u044e \u043f\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u0442\u0435\u0440\u043d\u0430\u0440\u043d\u044b\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430\u043c\u0438 \u0432 \u043f\u0435\u0441\u043e\u0447\u043d\u0438\u0446\u0435 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. <\/p>\n<p>\u0412\u044b \u043c\u043e\u0433\u043b\u0438 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <em>extends<\/em>. \u041a\u0430\u043a \u043e\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 ? <br \/>Extends \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442, \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u043b\u0438 \u0442\u0438\u043f T \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0439 \u0442\u0438\u043f TypeA, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u0443\u0431\u0435\u0436\u0434\u0430\u0435\u043c\u0441\u044f, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 T \u0442\u0430\u043a \u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f TypeA. <\/p>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <em>conditional types<\/em> \u0434\u043b\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u043f\u0440\u0438\u043c\u0435\u0440: <\/p>\n<pre><code>type TypeA = { id: string } type TypeB = { id: number }  type SafeType&lt;T extends TypeA> = T['id']  type ResultType1 = SafeType&lt;TypeA> \/\/ string type ResultType2 = SafeType&lt;TypeB> \/\/ ERROR: Type 'TypeB' does not satisfy the constraint 'TypeA'.<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0438\u0435 \u0442\u0438\u043f\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <em>constraints types<\/em> \u0438\u043b\u0438 \u0436\u0435 <em>&#171;\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0435 \u0442\u0438\u043f\u044b&#187;<\/em>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u0432\u044f\u0437\u043a\u0435 \u0441 typeof \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u0438\u0442\u044c \u0441\u0435\u0431\u044f \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043e\u0442 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0432 \u043d\u0430\u0448\u0438 \u043c\u0435\u0442\u043e\u0434\u044b. <\/p>\n<p><a class=\"anchor\" name=\"ict\" id=\"ict\"><\/a><\/p>\n<h3>Infer in conditional types<\/h3>\n<p>\u041a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e <em>infer<\/em> \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0441\u0441\u044b\u043b\u0430\u0442\u044c\u0441\u044f \u0438\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c.<\/p>\n<p>\u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443: <\/p>\n<pre><code>type TypeA = { id: string }  type TypeB = { id: number }  type InferType&lt;T> = T extends { id: infer P } ? P extends string ? string : number : any  type ResultType1 = InferType&lt;TypeA> \/\/ string type ResultType2 = InferType&lt;TypeB> \/\/ number type ResultType3 = InferType&lt;object> \/\/ any<\/code><\/pre>\n<p><a class=\"anchor\" name=\"rt\" id=\"rt\"><\/a><\/p>\n<h4>ReturnType<\/h4>\n<p>\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u043c\u0438 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<br \/>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u0433\u0434\u0435 \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u044b\u0439 ReturnType: <\/p>\n<pre><code>type CustomReturnType&lt;T> = T extends (...args: any[]) => infer P ? P : any  type ResultType1 = CustomReturnType&lt;() => void> \/\/ void type ResultType3 = CustomReturnType&lt;() => number> \/\/ number<\/code><\/pre>\n<p><em>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435:<\/em><\/p>\n<p><a class=\"anchor\" name=\"at\" id=\"at\"><\/a><\/p>\n<h4>ArrayType<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c union \u0442\u0438\u043f \u0438\u0437 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. <\/p>\n<pre><code>type ArrayType&lt;T> = T extends (infer Item)[] ? Item : T  const arr = [1, '2', null, undefined]  type ResultType = ArrayType&lt;typeof arr> \/\/ string | number | null | undefined<\/code><\/pre>\n<p><a class=\"anchor\" name=\"fat\" id=\"fat\"><\/a><\/p>\n<h4>FirstArgType<\/h4>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u044b \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0442\u0438\u043f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0441\u043e \u0432\u0442\u043e\u0440\u044b\u043c \u0438 \u0441 \u0442\u0440\u0435\u0442\u044c\u0438\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u0438\u043b\u0438 \u0436\u0435 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<pre><code>type CustomType&lt;T> = T extends (id: infer ID, ...args: any[]) => unknown ? ID : never  type ResultType1 = CustomType&lt;(id: string) => void> \/\/ string type ResultType3 = CustomType&lt;(id: number) => void> \/\/ number<\/code><\/pre>\n<p><a class=\"anchor\" name=\"cit\" id=\"cit\"><\/a><\/p>\n<h4>CustomInstanceType<\/h4>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u043c\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e InstanceType \u0438\u0437 TS. <\/p>\n<pre><code>type CustomInstanceType&lt;T> = T extends new (...args: any[]) => infer P ? P : any  interface ConstructorI {     new (arg: number): string }  type ResultType1 = CustomInstanceType&lt;ConstructorI> \/\/ string type ResultType2 = InstanceType&lt;ConstructorI> \/\/ string<\/code><\/pre>\n<p><a class=\"anchor\" name=\"off\" id=\"off\"><\/a><\/p>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>TypeScript &#8212; \u044d\u0442\u043e \u043c\u043e\u0449\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0435\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u043c \u0438 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u0438\u0435\u043c\u044b \u0432 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 TypeScript. <\/p>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0435 TypeScript &#8212; \u044d\u0442\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0438 \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0443\u0435\u0442\u0435\u0441\u044c, \u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u0431\u0443\u0434\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0441\u0432\u043e\u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445.<\/p>\n<p><a class=\"anchor\" name=\"mat\" id=\"mat\"><\/a><\/p>\n<h3>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f<\/h3>\n<p><a href=\"https:\/\/habr.com\/ru\/companies\/macloud\/articles\/559902\/\" rel=\"noopener noreferrer nofollow\">\u041e\u0441\u043d\u043e\u0432\u044b<\/a><br \/><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html\" rel=\"noopener noreferrer nofollow\">Utility Types<\/a><br \/><a href=\"https:\/\/www.typescriptlang.org\/cheatsheets\" rel=\"noopener noreferrer nofollow\">\u0428\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u043f\u043e TS \u0432 \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0430\u0445<\/a><br \/><a href=\"https:\/\/habr.com\/ru\/articles\/762902\/\" rel=\"noopener noreferrer nofollow\">TypeScript \u0438 \u0432\u0441\u0435 \u0447\u0442\u043e \u0442\u0435\u0431\u0435 \u043d\u0443\u0436\u043d\u043e \u0432 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435<\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/778190\/\"> https:\/\/habr.com\/ru\/articles\/778190\/<\/a><br \/><\/br><\/br><\/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-361909","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361909","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=361909"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/361909\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=361909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=361909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=361909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}