TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

TypeScript 4.3 Beta

40 pointsby flipchartabout 4 years ago

2 comments

dllthomasabout 4 years ago
Template string types are neat, and I&#x27;m excited to see what new functionality is afforded. I&#x27;ve already been able to use them for probably ill-advised magic (... not in production anywhere), like this type for &quot;strings with no leading or trailing spaces&quot;:<p><pre><code> type RSTRIP&lt;STR&gt; = STR extends `${infer T} ` ? RSTRIP&lt;T&gt; : STR; type LSTRIP&lt;STR&gt; = STR extends ` ${infer T}` ? LSTRIP&lt;T&gt; : STR; type STRIP&lt;STR&gt; = RSTRIP&lt;LSTRIP&lt;STR&gt;&gt;; </code></pre> which allows things like:<p><pre><code> function strip&lt;T extends string&gt;(str: T): STRIP&lt;T&gt; { return str.trim() as STRIP&lt;T&gt;; } function printStripped&lt;T&gt;(str: STRIP&lt;T&gt;) { console.log(str); } printStripped(&#x27;hi&#x27;); &#x2F;&#x2F;X printStripped(&#x27; hi&#x27;); printStripped(strip(&#x27; hi&#x27;));</code></pre>
eyelidlessnessabout 4 years ago
It’s odd to me that the Promise check is under the `strictNullChecks` flag. A Promise will never be `null` with that flag enabled.
评论 #26717584 未加载