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.

Learn how to unleash the full potential of the type system of TypeScript

589 pointsby NeekGerdover 2 years ago

35 comments

cogman10over 2 years ago
A great idea. Now, everyone that learns this stuff, show some restraint!<p>The drawback of a powerful type system is you can very easily get yourself into a type complexity mudhole. Nothing worse than trying to call a method where a simple `Foo` object would do but instead you&#x27;ve defined 60 character definition of `Foo` capabilities in the type system in the method signature.<p>Less is more.
评论 #32920746 未加载
评论 #32918728 未加载
评论 #32922056 未加载
评论 #32917647 未加载
评论 #32918387 未加载
评论 #32918968 未加载
评论 #32917631 未加载
评论 #32921249 未加载
评论 #32921662 未加载
评论 #32920438 未加载
评论 #32922538 未加载
评论 #32921394 未加载
somewhereoutthover 2 years ago
&gt; Over the years, the type system of TypeScript has grown from basic type annotations to a large and complex programming language.<p>Give someone (particularly a developer) the opportunity to build something complicated and undoubtedly they will. So now you have two problems, the complicated program that actually does some hopefully useful work, and another complicated program on top of it that fills your head and slows you down when trying to fix the first complicated program. You may say &#x27;ah yes, but the second complicated program validates the first!&#x27;. Not really, it just makes things more complicated. Almost all bugs are logic bugs or inconsistent state bugs (thanks OOP!), almost none are type bugs.<p>However, static analysis of existing code (in Javascript), without having to write a single extra character, may well have great value in indicating correctness.<p>Edit:<p>&gt; TypeScript&#x27;s type system is a full-fledged programming language in itself!<p>Run! Run as fast as you can! Note that this &#x27;full-fledged programming language&#x27; <i>doesn&#x27;t actually do anything</i> (to their credit they admit this later on)<p>Edit2:<p>&gt; [...] is a type-level unit test. It won&#x27;t type-check until you find the correct solution.<p>&gt; I sometimes use @ts-expect-error comments when I want to check that an invalid input is rejected by the type-checker. @ts-expect-error only type-checks if the next line does not!<p>What new level of hell are we exploring now??<p>I am genuinely afraid and I&#x27;m only halfway through this thing. What&#x27;s next? A meta type level language to check that our type checking checks??<p>&gt; 3. Objects &amp; Records<p>&gt; COMING SOON!<p>&gt; this chapter hasn&#x27;t been published yet.<p>Thank God, I am saved.
评论 #32919641 未加载
评论 #32919382 未加载
评论 #32919237 未加载
评论 #32919631 未加载
评论 #32919647 未加载
评论 #32919444 未加载
评论 #32919694 未加载
评论 #32928663 未加载
评论 #32921848 未加载
评论 #32922021 未加载
评论 #32919456 未加载
评论 #32936480 未加载
评论 #32919285 未加载
nsxwolfover 2 years ago
I currently have to occasionally contribute to a TypeScript codebase at work. I appreciate how much better it is than Javascript. When I write code as an outsider (Java and Go developer), I feel like I use the type system in sensible and readable ways. When I look at the code written by the native TypeScript experts in my company it is a bewildering, abstract, unreadable morass. I have no idea what&#x27;s going on half the time.
评论 #32921001 未加载
simlevesqueover 2 years ago
Wow I think I know a lot of Typescript but I&#x27;ll have to go through it because I&#x27;m always asked for ressources to get started and this one seem great.<p>I also recommend type-challenges: <a href="https:&#x2F;&#x2F;github.com&#x2F;type-challenges&#x2F;type-challenges" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;type-challenges&#x2F;type-challenges</a><p>It works great with the VSCode extension.
评论 #32924456 未加载
tunesmithover 2 years ago
Might as well ask here. On our teams, we have the occasional developer that is insistent on using Typescript in an OO fashion. This has always struck me as square peg round hole. Even though I come from an OO background, Typescript strict settings really seem to push me in a direction of using interfaces and types for type signatures, and almost never classes, subclasses, instantiated objects. I don&#x27;t have a very good answer for &quot;yeah, but what about dependency injection&quot;? though. Any thoughts from anyone?
评论 #32918918 未加载
评论 #32918433 未加载
评论 #32918774 未加载
评论 #32919536 未加载
评论 #32919678 未加载
评论 #32919526 未加载
评论 #32922667 未加载
评论 #32918019 未加载
评论 #32922066 未加载
评论 #32920964 未加载
评论 #32923471 未加载
评论 #32918937 未加载
评论 #32918952 未加载
Rapzidover 2 years ago
I wish this issue would get addressed: <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;vscode&#x2F;issues&#x2F;94679" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;vscode&#x2F;issues&#x2F;94679</a><p>Showing fully resolved types in Intellisense would be the single largest usability enhancement they could make for me right now..
评论 #32917138 未加载
评论 #32916830 未加载
agentultraover 2 years ago
Nice course, and nice site.<p>Although, as a Haskell developer, I am curious what type system TS is using (System F? Intuitionist? etc) and what limitations one can expect. Aside from the syntax of TS being what it is, what are the trade-offs and limitations?<p>I was under the impression, and this was years ago -- things are probably different now?, that TS&#x27;s type system wasn&#x27;t sound (in the mathematical logic sense).
评论 #32916865 未加载
评论 #32918087 未加载
323over 2 years ago
One think I struggled a lot with until I got it is that the TypeScript types and the JavaScript code live in totally separate universes, and you cannot cross from the type world to JavaScript values because the types are erased when transpilling - meaning they can&#x27;t leave any trace.<p>This means that it&#x27;s impossible to write this function:<p><pre><code> function isStringType&lt;T&gt;(): boolean { return ... } const IS_STRING: boolean = isStringType&lt;string&gt;(); </code></pre> At best you can do something like this, which is inconvenient for more complex cases:<p><pre><code> function isStringType&lt;T, IsString extends boolean = T extends string ? true : false&gt;(isString: IsString): boolean { return isString } const IS_STRING_1: boolean = isStringType&lt;string&gt;(true); &#x2F;&#x2F; compiles const IS_STRING_2: boolean = isStringType&lt;string&gt;(false); &#x2F;&#x2F; type error </code></pre> You basically need to pass the actual result that you want in and just get a type error if you pass in the wrong one. Still better than nothing.<p>Link if you want to play with it online: <a href="https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;play?#code&#x2F;GYVwdgxgLglg9mABDAzgZSgJxmA5gFQE8AHAUwB58AaRASXSx10VIA8pSwATFRAIzhwANqQCGSALyJ8Ldpx6IUjPIgD8iLCFKIAXImCihKUgD4AFKgzY8e+laYBKPQOFikAb0SZSUEJiSWyswAvgBQoRAISnRoAPpo+ABKtAByAOKxAIzOgiLiiFKB1gQkFErF5pqkDgDciAD09YiRALbEMCIoEVFQMfFJqRkATDmu+YUMxURk5OVM5gZG1XWNGqUsmJhwmKFAA" rel="nofollow">https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;play?#code&#x2F;GYVwdgxgLglg9mABDA...</a><p>Put another way, you can&#x27;t do reflection with TypeScript.<p>You can write that function in C++ templates, and I naively assumed that it&#x27;s possible in TypeScript too, since from my observations TypeScript allows complex typing to be expressed easier in general than C++.
评论 #32918377 未加载
评论 #32918208 未加载
评论 #32917800 未加载
评论 #32918622 未加载
评论 #32918184 未加载
评论 #32918466 未加载
YoannMoinetover 2 years ago
I particularly love the interactivity of the training. So polished.<p>Can&#x27;t get enough of the fireworks!
guggleetover 2 years ago
Seems like a good start, but there are a lot of interesting offerings in the introduction that really don&#x27;t exist in the content so far.<p>Maybe finishing one of the more advanced chapters would be enough to lure people who are more experienced to check back on progress &#x2F; pay &#x2F; whatever you want traffic for.
amadeuspagelover 2 years ago
Looks cool. I like how it gives immidiate feedback, but doesn&#x27;t feel constraining or arbitrary. Is there are more basic tutorial in a similar style?
kbr-over 2 years ago
Is there any place I could report issues or ask questions about the course other than Twitter?<p>If the author is reading this, the proposed solution to the `merge` challenge is:<p><pre><code> function merge&lt;A, B&gt;(a: A, b: B): A &amp; B { return { ...a, ...b }; } </code></pre> That&#x27;s the &quot;obvious&quot; solution, but it means that the following type-checks:<p><pre><code> const a: number = 1; const b: number = 2; const c: number = merge(a, b); </code></pre> That&#x27;s not good. It shouldn&#x27;t type check because the following:<p><pre><code> const d: number = { ...a, ...b }; </code></pre> does not type check.<p>And I don&#x27;t know how to express the correct solution (i.e. where we actually assert that A and B are object types).<p>Also, looking forward to further chapters.
评论 #32917961 未加载
评论 #32918738 未加载
d4mi3nover 2 years ago
I&#x27;m always impressed at how much the type system in TS is capable of. The provided examples remind me of what I&#x27;d expect in something like Rust; it brings me joy that we can do this sort of stuff in our frontend code and tooling these days.<p>We have come far.
评论 #32916700 未加载
bwestergardover 2 years ago
This is fantastic! I&#x27;ve often had to advise coworkers to read documentation for OCaml or Rust to learn idiomatic, functional, statically typed programming. It&#x27;s great to see a Typescript specific resource with exercises.
downvoteme77221over 2 years ago
my HTML blog only has 30 lines of JS. i didn&#x27;t need a framework and i certainly don&#x27;t need types. grumble grumble web should just be simple html javascript grumble serverside render in my pet language grumble &#x2F;s
评论 #32918797 未加载
评论 #32917237 未加载
Myrmornisover 2 years ago
This looks fantastic, not just for people learning Typescript, but I&#x27;d think it would be useful (when completed) as an introduction to generics and type-level thinking etc for lots of newcomers to those areas.
SeriousMover 2 years ago
Types are here to help but with this power comes great responsibility. The deeper you allow an input to be used in your library&#x2F;method the more it makes sense to put a well defined type contract on it. Exhaustive type definitions may show that the author doesn&#x27;t have a understanding of a required interface, abstractions or pattern to use. I use c# professionally for 20 years and I love the type system. It helps to tell the contract about the types and functions you expect yet you can overcomplicate things if you really about to. c# makes it &quot;hard&quot; to create method type definitions and you should use interfaces to achieve the contract definition. This helps to avoid the inline type definition as it&#x27;s done in typescript. My approach is to use types in typescript as I&#x27;m used to in c#.
Benjamin_Dobellover 2 years ago
One of the worst things about Next.js, Remix etc. is their file system driven routes. I really wish these frameworks would stop relying so much on hidden magic. Conventions are good, but as to why those conventions aren&#x27;t <i>in code</i> is quite peculiar.<p>Previously, I wrote my route definitions with types for both path params and query params in one file, and used TypeScript to enforce that the equivalent back-end definitions (async loaders etc.) and front-end definitions (React components) were kept in sync.<p>When I first implemented this in a previous project, I found <i>many</i> instances where routes were expecting query params but they were being dropped off (e.g. post login redirects).<p>Supporting things like parameters for nested routes certainly means the TS types themselves are non-trivial, but they&#x27;re the kind of thing you write (and document) once, but benefit from daily.<p>Examples of stuff that can and <i>should</i> be 100% type checked:<p><pre><code> &#x2F;&#x2F; ... template: { path: idPath(&quot;&#x2F;template&quot;), subroutes: { edit: { path: () =&gt; &quot;&#x2F;edit&quot; }, remix: { path: () =&gt; &quot;&#x2F;remix&quot; }, }, }, onboarding: { path: () =&gt; &quot;&#x2F;onboarding&quot;, query: (params: { referralCode?: string }) =&gt; params, subroutes: { createYourAvatar: { path: () =&gt; &quot;&#x2F;createyouravatar&quot; }, }, }, &#x2F;&#x2F; ... </code></pre> Routing:<p><pre><code> &#x2F;&#x2F; Path params navigate(routes.template.edit({ id: props.masterEdit.masterEditId })); &#x2F;&#x2F; No path params, just query params (null path params) navigate(routes.onboarding(null, { referralCode })) &#x2F;&#x2F; Nested route with query params (inherited from parent route) navigate(routes.onboarding.createYourAvatar(null, { referralCode })) </code></pre> React hooks:<p><pre><code> &#x2F;&#x2F; Path params const { id } = useRouteParams(routes.template.edit) &#x2F;&#x2F; Query params const { referralCode } = useRouteQueryParams(routes.onboarding); </code></pre> API routes:<p><pre><code> &#x2F;&#x2F; ... play: { path: () =&gt; &quot;&#x2F;play&quot;, subroutes: { episode: { path: idPath(&quot;&#x2F;episode&quot;), }, }, }, &#x2F;&#x2F; ... </code></pre> Relative route paths (for defining nested express routers):<p><pre><code> const routes = relativeApiRoutes.api.account; router.post(routes.episode(), async (req: express.Request, res: express.Response) =&gt; {</code></pre>
评论 #32920426 未加载
throw10920over 2 years ago
&gt; Expect&lt;Equal&lt;Hello, &quot;World&quot;&gt;&gt;<p>This seems like a great time to bring up &quot;Why Static Languages Suffer From Complexity&quot;[1], which explains the &quot;statics-dynamics biformity&quot; that leads to languages like TypeScript that are actually <i>two</i> languages: the runtime one and the compile-time type-system one.<p>[1] <a href="https:&#x2F;&#x2F;hirrolot.github.io&#x2F;posts&#x2F;why-static-languages-suffer-from-complexity" rel="nofollow">https:&#x2F;&#x2F;hirrolot.github.io&#x2F;posts&#x2F;why-static-languages-suffer...</a>
robertwt7over 2 years ago
Really interesting articles, will definitely have a look at night.<p>After so many years of JS programming, moving to a company that uses TS extensively (in a huge scale) feels life changing. You don&#x27;t even know the effect until you use it daily. Even so, daily usage of typescript at a large web application doesn&#x27;t seem to be using its full potential. I feel like libraries creator and maintainer use them more in the definitions that they created (i.e redux toolkit type is mind blowing).<p>Thanks for creating this lesson, it will definitely teach me a lot
aussiesnackover 2 years ago
Looks good. Given the course is unfinished, it&#x27;s a shame the only way to get updates is via a third party corporate web site (&quot;Twitter&quot;). RSS would be the obvious way to go.
评论 #32933825 未加载
HellsMaddyover 2 years ago
This is great. Can you please create an email list where we can sign up to be notified when new chapters are available? If I follow you on Twitter, I will invariably miss any announcements.
kaladin_1over 2 years ago
Just came here to say that this is really nice. Fantastic way to play with Typescript Types even for people with decent knowledge of Typescript as I would consider myself.<p>Particularly enjoy the confetti :)
cercatrovaover 2 years ago
Speaking of types, what are your thoughts on fp-ts if you&#x27;ve used it? It brings functional programming concepts like in Haskell such as monads into TypeScript.
评论 #32917114 未加载
评论 #32917288 未加载
评论 #32917230 未加载
评论 #32918819 未加载
vladdeover 2 years ago
Similar to the paid tool <a href="https:&#x2F;&#x2F;www.executeprogram.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.executeprogram.com&#x2F;</a> , which goes very much more in-depth with TypeScript&#x27;s type system (as well with other languages)
AkshatJ27over 2 years ago
This reminded me of the Typescript type-level Quine. <a href="https:&#x2F;&#x2F;youtu.be&#x2F;UzE6d1ueT_E" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;UzE6d1ueT_E</a>
theteapotover 2 years ago
Didn&#x27;t know `Expect` existed. Can&#x27;t find it docs?
评论 #32918195 未加载
jherikoover 2 years ago
reminds me of C++ templated types being used for similar... except in this case there is no performance advantage from removing run-time logic by force.<p>this kind of stuff is often confusing when working with teams. using simple dumb stuff is always the better option when you can.
nonethewiserover 2 years ago
This is a really nice website.
willthefirstover 2 years ago
Great content. Give it me to me as a daily-dose newsletter :)
classifiedover 2 years ago
Is TypeScript&#x27;s type system Turing-complete?
评论 #32917793 未加载
arberxover 2 years ago
There are only 3 chapters so far...
Ayc0over 2 years ago
I love the content!!
theteapotover 2 years ago
Course is probably great, but I find it really weird and unnecessary to describe Typescript type system as Turing complete. Who cares?
评论 #32918114 未加载
gitowiecover 2 years ago
Hi n I have a question. I will go as simple and short as possible. I joined a small team working on the internal invoicing tool. Backend is Spring. Front-end is ExtJS used for me in very peculiar way. It emulates Java classes, there are Ext.define declarations with FQN names eg: &quot;com.projectName.ds.Board.ui.extjs&quot; (as string, casing important) Then in the code this class is instantiated by its FQN but used as identifier eg: var Board = new com.projectName.ds.Board.ui.extjs(); There are also a lot of FQNs with short namespaces, different are associated with business short names and other like Dc, Ds, Frame belong to code architecture domain (data controller, data store, a frame on the screen). How I could use typescript to improve developer experience here? I&#x27;m from the react world, I programmed 4 years only in typescript, react, node and mongo. Thanks!