TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Is it worth it to invest in learning TypeScript?

61 点作者 pandatigox超过 3 年前
I don't do much frontend programming so I've been mainly using Javascript. However, more and more libraries are using Typescript which makes me think that I should make the jump. But I don't understand how adding types will suddenly make frontend coding more efficient. I tried researching for pros and cons but nothing came up.

35 条评论

nayuki超过 3 年前
TypeScript is the biggest factor that made JavaScript coding bearable to me. Before that, I was collapsing under the weight of my ~1000-line JavaScript programs, because I couldn&#x27;t mentally keep track of types and names. Similarly, revisiting my old JavaScript programs after a year, it was tedious to reread my code to infer the types and structures.<p>I know the rules of JavaScript, including all the weird type coercion stuff, undefined, etc. I know how to write correct programs, but I hate the human effort of having to jump around my codebase to recheck definitions and to double- and triple-check my work for silly mistakes.<p>Writing TypeScript code makes me do a bit more work upfront to annotate types. But in exchange, the compiler helps me tremendously in tediously checking all the types and names. Functions with types are more self-documenting than untyped functions; it&#x27;s easier to read and figure out what a function does at a glance.<p>In a sense, compile-time type-checking is more powerful than unit tests because unit tests only cover functions and branches and (run-time) types that are actively taken, whereas type checking applies to all code all the time.<p>Before discovering TypeScript, I used this workaround a few times: For complicated pure algorithm &#x2F; data structure stuff (not DOM or user interface stuff), I would prototype the program in Java with all the glorious types, test the heck out of it, and then translate it line by line to JavaScript.
bryans超过 3 年前
If you want a job writing JS in 2021, then yes, you should be proficient with TS. That said, there&#x27;s not really a whole lot to it, other than pre-defining types for function arguments and objects using a couple extra syntax elements. It&#x27;s actually less efficient when writing, but it&#x27;s significantly more efficient when testing and debugging, because issues tend to become glaringly obvious. And many developers will attest that it helps force them to write better initial code.<p>That said, I stick with JS and I&#x27;m perfectly happy doing so.
评论 #29140792 未加载
cardanome超过 3 年前
If you use JS to sprinkle in some jQuery style interactivity on an otherwise static website, then TypeScript is not worth it because of the additional overhead. For anything more complex and most single page applications, yes do use TS.<p>TS offers a better developer experience (better code completion in your IDE), types help document your code and refactoring will be less painful. Also you gain some level of type safety (though less than languages with a sound type system).<p>Drawbacks are slow compile times and cryptic error messages and general having a additional tool that adds complexity. Also the level of type safety is dependent on how disciplined you are (though some linting rules might help).<p>There are languages with much better type systems and faster compile times that transpile to JS that might also be worth noting like Elm, ReScript, Purescript and so on but TS has of course the best migration story with the &quot;just JS but with types&quot; promise.<p>So generally, yes learn it, use it.
aecorredor超过 3 年前
100% yes. As someone who did JavaScript only for a while, once you make the switch, you’ll never want to go back.<p>The things you’ll get: 1. It’s going to force you to think more about how you structure your program. You’ll probably end up with a cleaner design. 2. It’s going to make you code faster because of the awesome autocomplete functionality you get out of the box (at least with VSCode). 3. It’s going to give you much more confidence that your code won’t break at runtime. There’s still a chance it will (write unit tests), but it’ll be much less frequent. 4. Types are a kind of documentation, so your future self will thank you.
Fannon超过 3 年前
Just to add an aspect that I haven&#x27;t seen so far.<p>TypeScript makes your interfaces and their documentation much more explicit than with vanilla JS. If you work on your code in a team, you actually want to very clearly document the inputs and outputs of your functions and the types of your data structures. Doing this with JSDocs is cumbersome and error prone in my experience and actually more work than just using TypeScript.<p>Having explicit types with TypeScript helps both machines (better &#x2F; safer refactoring) and the humans to reason about the code.<p>So the bit of extra effort to add the types is very well invested. And keep in mind that developers spend much more time reading code than writing it.
MH15超过 3 年前
Typescript isn’t to make your code run faster- it’s to make it faster to write your code, especially in a team environment. It’s super nice to know what types that function your coworker wrote will accept, or what is in that struct. It’s really just a tool to boost programmer confidence, and a must-have for any large new project IMO.
评论 #29140399 未加载
dgb23超过 3 年前
&gt; But I don&#x27;t understand how adding types will suddenly make frontend coding more efficient.<p>It won&#x27;t. The runtime is not affected. All it does is add meta-data to your program so static analysis gets more information about what your code is doing AKA catch type errors.<p>Type errors in frontend code are typically rare in comparison to state, timing and synchronization errors for example.<p>Learning the basics of TS is easy if you know any other typed language. However some of the concepts are fairly &quot;unique&quot; to TS or rather unusual in other typed languages.<p>Studying TS won&#x27;t hurt you, you can read more code and interact better in TS codebases. However, if you plan on actually using it I very much recommend you just pick a useful subset of the features. It is already a fairly bloated language which can distract you from doing stuff that matters.
评论 #29141111 未加载
评论 #29140885 未加载
peterkelly超过 3 年前
Static typing reports certain classes of error in your program at compile (aka &quot;transpile&quot;) time. This reduces the number of tests you need to write and gives you more confidence about the correctness of your code than is possible with dynamic typing.<p>The alternative is finding out some time later (possibly in production) that your function that had always been assuming the arguments were non-null strings suddenly generates an error when it gets passed in null or a number or object or similar.<p>TypeScript is a <i>huge</i> productivity boost for JavaScript programmers (and similarly mypy for Python). There&#x27;s no way I&#x27;d be happy working with either language without these tools. TypeScript is also useful on the backend, if you&#x27;re using Node.
评论 #29142571 未加载
cygned超过 3 年前
Others will talk about the benefits, so let me give you a few gotchas from us:<p>1. It does not replace type checks at runtime (in integration layers)<p>2. Compilation and startup (in backend land) is slow<p>3. Some tooling seemingly caches (webpack? ts-node?), we regularly run into situations where rebuilding locally works but then building for prod fails with type errors
评论 #29140729 未加载
评论 #29140914 未加载
scanr超过 3 年前
The payoff is reduced cognitive load. Essentially you’re offloading a bunch of it to the type system at the cost of type annotations. A simple example:<p><pre><code> const add = (a, b) =&gt; a+b </code></pre> can do quite a few surprising things depending on the types of a and b.<p>Whereas how this will behave:<p><pre><code> const add = (a: number, b: number) =&gt; a+b </code></pre> is far easier to keep in your head. Multiply that effect by 100k lines and the TypeScript program becomes a lot more fun to build on because you get to spend less time thinking about edge cases.
notsrg超过 3 年前
Over the last couple years I&#x27;ve worked at a F500 company with no TS and a startup which auto generated TS types (so i.e. 100% TS) and guess which company moved faster? The F500 you literally had to get on multiple meetings across multiple teams just to figure out what values something might be. It took months to onboard new engineers and the bus factor was insanely high.<p>Once you&#x27;ve worked in a codebase with consistent TS usage, it&#x27;s very, very painful to go back to vanilla JS, so imo yes, TS is worth the investment.
acemarke超过 3 年前
Yes, absolutely, 100%.<p>I wrote a post a couple years ago describing my journey learning and using TS, with a set of takeaways at the end:<p><a href="https:&#x2F;&#x2F;blog.isquaredsoftware.com&#x2F;2019&#x2F;11&#x2F;blogged-answers-learning-and-using-typescript&#x2F;#lessons-opinions-and-takeaways" rel="nofollow">https:&#x2F;&#x2F;blog.isquaredsoftware.com&#x2F;2019&#x2F;11&#x2F;blogged-answers-le...</a><p>I said then that &quot;I refuse to write any more app code that isn&#x27;t TS&quot;, and that&#x27;s even more true today.<p>TS is not perfect. It does add overhead. But, it adds a huge amount of value and safety to your app. The goal is to use it pragmatically, such that Value &gt; Overhead.<p>*edit*<p>I&#x27;ll add a bit of additional context here based on my experiences since then.<p>Typing application code is usually relatively straightforward. I&#x27;ll use React and Redux as an example, because that&#x27;s A) what I use at work, and B) I maintain Redux.<p>Per our Redux TS Quick Start page [0], you&#x27;d start with a few lines to infer the TS types of `RootState` and `AppDispatch` from the store, and create pre-typed React-Redux hooks (which are _much_ easier to type than the legacy `connect` API) to save from repeating those types in components. For each slice reducer, you&#x27;d add a TS type for the slice&#x27;s state, and declare the type of each action&#x27;s contents as `action: PayloadAction&lt;string&gt;`, etc. For React components, add a type declaration for the props of each component, and add types to function arguments as necessary (change handlers, etc). The React TypeScript CheatSheet [1] is a fantastic resource for learning how to use TS + React together. Also be sure to type any data coming into the system, such as API responses, and any other &quot;business logic&quot; you might have.<p>Those should get you 80%-ish types coverage without too much effort, and all you really need to know is how to declare types for objects+primitives, and a few bits from the React and Redux lib types.<p>I also strongly disagree with many of the &quot;recommended&quot; TS-related linting rules, like &quot;always declare a return type for every function&quot;. It&#x27;s not _wrong_ to have those, but in my experience TS works best when you can let the compiler infer as much as possible.<p>I still firmly believe that for _app_ development, it&#x27;s not worth spending hours and hours arguing with the compiler to come up with a &quot;perfect 100% correct&quot; type definition for more complicated scenarios. If you know that a function takes `TypeA` as an input and returns a `TypeB`, but there&#x27;s really complex transformations inside and it&#x27;s really hard to convince the compiler what you&#x27;re doing is correct, feel free to do whatever `as` casting or `&#x2F;&#x2F; @ts-ignore` you need to in the middle.<p>Library types, on the other hand, are the opposite case. Library APIs are often highly generic in both the &quot;use case&quot; and &quot;TS types&quot; senses. There, it _is_ worth spending the time to come up with very complex typedefs with tons of generics and conditional types, because those are often needed to make the _end user experience_ a lot easier. Try looking at the types for Redux Toolkit, React-Redux, or Reselect [2] [3] [4] - our types are complex, so _your_ code doesn&#x27;t have to be.<p>I spent a ton of time just yesterday trying to make improvements to the Reselect types, because as a maintainer I want our users to have a good experience. But for you as an app dev, it should be a lot simpler. The payoff is that you get all the nice benefits of TS: static detection of common errors like typos and mismatched fields, documentation of your data structures, intellisense, confidence in refactoring, less need to write repetitive &quot;is this argument the right type?&quot; logic, and much more.<p>[0] <a href="https:&#x2F;&#x2F;redux.js.org&#x2F;tutorials&#x2F;typescript-quick-start" rel="nofollow">https:&#x2F;&#x2F;redux.js.org&#x2F;tutorials&#x2F;typescript-quick-start</a><p>[1] <a href="https:&#x2F;&#x2F;react-typescript-cheatsheet.netlify.app&#x2F;" rel="nofollow">https:&#x2F;&#x2F;react-typescript-cheatsheet.netlify.app&#x2F;</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;redux-toolkit&#x2F;blob&#x2F;v1.6.2&#x2F;packages&#x2F;toolkit&#x2F;src&#x2F;createAction.ts#L10-L221" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;redux-toolkit&#x2F;blob&#x2F;v1.6.2&#x2F;package...</a><p>[3] <a href="https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;react-redux&#x2F;blob&#x2F;v8.0.0-alpha.1&#x2F;src&#x2F;types.ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;react-redux&#x2F;blob&#x2F;v8.0.0-alpha.1&#x2F;s...</a><p>[4] <a href="https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;reselect&#x2F;blob&#x2F;v4.1.2&#x2F;src&#x2F;index.ts#L153-L205" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;reduxjs&#x2F;reselect&#x2F;blob&#x2F;v4.1.2&#x2F;src&#x2F;index.ts...</a>
评论 #29140714 未加载
SkyPuncher超过 3 年前
The &quot;pros&quot; of Typescript are basically the same as using a typed language.<p>* If you don&#x27;t know a typed language - learn one. TS is probably the easiest place to start.<p>* If you know a typed language, TS is just going to be a different flavor of that.
is0tope超过 3 年前
If you intend to work with anything front end or node related the 100% yes. I was skeptical at first, but would say I am fully converted.<p>The tipping point for me was when I tried to implement an order book (finance data structure) in typescript, and got it running pretty much on the first run. In the past, I would always screw up one of the structures, such as forgetting to pass an argument, or a field, or something similar.<p>The type system is powerful, but gives you escape hatches such as &quot;any&quot; which is useful when you run up against eg. interacting with the DOM where these things can get quite annoying.
hliyan超过 3 年前
Typescript doesn&#x27;t necessarily make your code any more stable than equivalent vanilla JavaScript that validates function arguments properly. However, it does make the code more terse and the types more apparent. It adds some build time complexity and also requires (depending on how you configure it) that you define types for every little thing (e.g. argument objects passed into functions). In IDEs, you tend to get better autocomplete on Typescript than JavaScript. That said, I still prefer JavaScript.
评论 #29140613 未加载
评论 #29140487 未加载
评论 #29140411 未加载
评论 #29140566 未加载
评论 #29140392 未加载
fregante超过 3 年前
Think of TypeScript as writing JavaScript with inline tests: It takes longer but it produces &quot;safer&quot; code (with fewer bugs)<p>Is it worth learning it? I&#x27;m sure type-less job offers will continue to exists in large numbers, so if you don&#x27;t like it then don&#x27;t study it.<p>Note: TS is generally not referred to as &quot;testing&quot;, this is just my simplification. I&#x27;ve been using TS for years for everything and it has avoided a lot of bugs — which is something I notice when porting projects to TS.
评论 #29140690 未加载
65超过 3 年前
It&#x27;s probably worth it. I always use Typescript with React projects, as typing your props will make things a lot clearer. If you&#x27;re using VSCode, the intellisense is very useful.<p>However, if you&#x27;re just making a node CLI or writing a small Lambda or something, Typescript is probably not worth it. I don&#x27;t believe you need Typescript with absolutely every project, but for the more complex frontend projects it will end up saving a lot of time.
评论 #29140907 未加载
readonthegoapp超过 3 年前
I don&#x27;t think there is much of a learning curve, so might as well learn it.<p>I think I &#x27;learned it&#x27; because that&#x27;s just what you did to use the catastrophe known as Angular.<p>I thought we might have the chance to move from a javascript to a typescript world, but it seems it never quite fully caught on. Maybe everyone is still just trying to figure out how to get rid of javascript, or get rid of node, or support typescript in the browser, or...
eropple超过 3 年前
Heck yeah. I write a lot of Node and a decent bit of frontend-du-jour, and at this point, TypeScript is a requirement--not using it is a <i>bug</i>, on systems I touch.<p>&quot;More efficient&quot;? It&#x27;s this: <i>I make fewer mistakes</i>. The tools are better equipped to help me by pointing out earlier when I&#x27;ve done something wrong because my types are detailed and specific. It also means I take a minute to think my problems through up front (both in terms of object typing and in terms of necessary function arguments) before I start whaling away at logic that may or may not actually reflect what those things <i>are</i> by the time I&#x27;ve got it sketched out.<p>Think of your data first, then the logic that describes the interrelation of your data, and things like TypeScript become a gimme. (And you&#x27;ll learn lessons you can take forward with you to other environments, too.)
prohobo超过 3 年前
You use TypeScript with intellisense to tell you the types and shapes of variables&#x2F;objects. It prevents you from accidentally, say, assigning a number to a variable that was previously a string. It prevents you from assigning a prop to an object in the wrong place.<p>That&#x27;s all it is, and it works.
makstaks超过 3 年前
If you are writing code by yourself and your code base is relatively small, then I agree it&#x27;s hard to see the benefits at first.<p>If your team and code base grows, one of the first things you&#x27;ll notice is a lot of time wasted trying to figure out what parameter types a method takes. If you can imagine, a new person onboarding on your code base and having to figure this out constantly and then forgetting, is pretty inefficient.<p>TypeScript solves that problem since you declare types explicitly in code. The new person will know exactly the parameter types, the IDE can support you more intelligently, and you&#x27;ll get upfront compile time errors.<p>The downside is the initial setup and the extra effort of declaring types, so it comes with a cost, but personally I think the benefit outweighs the cost.
评论 #29141365 未加载
flohofwoe超过 3 年前
Neither JS nor TS is my &#x27;main language&#x27;, but one big advantage of using Typescript I noticed immediately is that editors can provide much more Intellisense information while typing and immediately show error squiggles if a function call parameter isn&#x27;t what the API expects (same as in any other statically typed language of course, but for writing &#x27;web code&#x27; I thought that&#x27;s a pretty big deal). This probably also works with WebIDL for regular JS APIs (as I said, JS is not my main language), but in TS this just works automatically also for your own APIs.
kgin超过 3 年前
Typescript is for big code bases and big teams.<p>The smaller your project, the more pointless typescript will feel. Working alone on a small project, it’s a LOT of extra stuff on the screen to avoid a very specific kind of bug.
评论 #29140747 未加载
评论 #29141243 未加载
评论 #29140772 未加载
masalachai超过 3 年前
Does anyone have suggestions or experience moving a fairly large JS codebase to TS? There are libraries that will auto generate TS code but not sure what the pluses and minuses are when doing that on a live, shared codebase.<p>We asked ourselves the exact question and 75% of the team is in favor of moving to TS, so this thread is great validation!
评论 #29141197 未加载
评论 #29141142 未加载
sibeliuss超过 3 年前
There&#x27;s not much to learn at first! That&#x27;s what `any` is for. You can scale up your learnings over time. It&#x27;s one of the amazing benefits of the language, that there&#x27;s no difficult on-ramp to start using it now. Just flipping it on will give you so much without even adding complex types.
geenat超过 3 年前
Keep in mind TypeScript is just types, an added compile step, and branding.<p>The ECMAScript spec will probably supersede TypeScript in the near future anyway. This has happened historically with other dead Javascript dialects (example: when classes were included in ES6).
评论 #29140536 未加载
评论 #29140967 未加载
评论 #29141009 未加载
评论 #29140592 未加载
simonlc超过 3 年前
Yes. It&#x27;s a tool that helps you. It has its quirks, just like any language, but they are outweighed by the benefits.<p>I was hesitant at first too, but I am sold on it now. Most libraries provide types too, so the entire JS ecosystem has basically adopted it as well.
yakshaving_jgt超过 3 年前
It depends on your motivation. If you want to actually work with TS, then of course learn TS. If you want to improve how you think about types in a more general sense (which is definitely a wise investment), then I&#x27;d learn Elm instead.
friedman23超过 3 年前
I just recently finished my job search and a *lot* of the startups I interviewed at were using typescript. So if you want to do frontend work at unicorn startups I suggest you get experience with that.
halotrope超过 3 年前
From my experience it is an absolute no-brainer. If you have used any typed language before I would say you can get productive in about an hour.
johncoltrane超过 3 年前
If it is good for your career, then it is worth the investment. Whether the tech in question makes sense or not is pretty much irrelevant.
JoiDegn超过 3 年前
I&#x27;ve found that the only real downside to typescript is that you are adding yet another layer on your at times unstable JavaScript environment.
dorianmariefr超过 3 年前
Depends, yes, no. It&#x27;s mostly for people using VS Code to have auto-completion and errors on null values
Lapsa超过 3 年前
I don&#x27;t like it.
评论 #29140754 未加载
simlevesque超过 3 年前
Yup.