7 ms·
The Concise TypeScript Book
- golergka 3y agoIt feels like the same information is repeated often 2, sometimes even 3 times. And overall, I don't think this book has anything more than what's already in official docs.
- tough 3y agoI thought it was this official "Handbook" https://www.typescriptlang.org/docs/handbook/intro.html https://www.typescriptlang.org/docs/handbook/intro.html
- eBombzor 3y agoThere's also this one (not official) https://basarat.gitbook.io/typescript/ https://basarat.gitbook.io/typescript/
- graypegg 3y agoI would actually love a “Typescript: The good parts” as a spiritual sequel to Javascript: The good parts. [0] Something that starts at the basics and builds upon that knowledge layer by layer like this handbook, but doesn’t dedicate more than a page to any one topic, and doesn’t repeat itself. Not a tutorial, but more like a glossary ordered by abstraction. Plus, there’s a lot of Typescript I would consider not part of the “good stuff”, we can leave out enums. [0] https://www.goodreads.com/book/show/2998152-javascript https://www.goodreads.com/book/show/2998152-javascript
- HWR_14 3y agoWhat's wrong with enums? Enum types in general are very useful.
- tough 3y agoSomeone mentioned they shouldn't be in TS altogether in another thread shrugs I like em too
- triyambakam 3y agoIf you mean traditional enums, called unions in Typescript and represented by the pipe symbol, then nothing. But if you mean the named Enum keyword in Typescript, then the problem is that they emit runtime code and aren't type safe.
- mostlylurks 3y ago> What's wrong with enums? They don't compile down to just the same code with the type annotations and definitions removed, which is often considered to be the ideal that typescript should follow. Instead, they compile down to some more complicated blob of javascript. People critique and avoid typescript's namespaces for the same reason. > Enum types in general are very useful. You can use unions for the same purpose without the aforementioned drawback, and unions have other benefits which typescript's enums don't offer, like support for more complex data types, which makes unions closer to set of features more modern languages tend to offer via enums, while making enums closer to the barebones enums found in languages like C and C++ (effectively just a set of named constants enclosed into a type).
- HWR_14 3y agoI guess I don't value the "TS transpiles to JS 1-to-1" property very much. Unions are useful for quite a few things. But I literally was talking about the barebones (and thus simple to write) C/C++ style enums. Where I know it can be one of several primitive values and not have to spend the time creating a class for each possible value.
- jahewson 3y agoAmongst their issues is the issue of enum vs const enum. These compile to different outputs and support different behaviors. It’s not possible to compile a TS file that uses them without looking at the enum declaration to see if it’s const. It’s no longer possible to strip the types and have something that any JS bundler can understand.
- chrisweekly 3y agoTry Dan Vanderkam's "Effective Typescript: 62 Specific Ways to Improve YourTypeScript" (O'Reilly). I'm no TS guru, but IMHO it's pretty well-grounded in first principles.
- orta 3y agoPerhaps it is LLM generated, as git says it is ~3 months old - but it links to a page on the TypeScript website which was removed a long time before that, https://www.typescriptlang.org/dt/search https://www.typescriptlang.org/dt/search
- fullstackchris 3y agoyeah I took a look and couldnt help but feeling like it was a laundry list with minimal examples and not always telling you why or when you would use a certain feature of TypeScript. Example: TypeScript _does_ enable you to overload functions, but overloading functions is nearly never the best way to do something, often cited as a spaghetti code pattern
- synergy20 3y agorecently there are a few stories about "using jsdoc for static typing instead of typescript as a JS programmer", I wonder when JS can add support of static typing directly as Python did, that will be nicer without learning a new language like TS. TS is great, I just never could find time to learn one more language.
- idontwantthis 3y agoThe best thing with TS is you can apply as much or as little as you want. Start by adding the compiler to your workflow and get it to work without making any changes to your code. Then add a couple types and see how it goes.
- eropple 3y agoThe projects that are using JSDoc for typings are doing so because they are way, way out on the edge in ways that stress Typescript compilers. They're doing weird stuff. It isn't a recommended path unless you absolutely need it; it's sacrificing a lot in order to yield faster round-trip time on codebases that have no other option. Typescript isn't really "a new language", either. It's a pretty mild superset.
- madeofpalk 3y ago> because they are way, way out on the edge in ways that stress Typescript compilers I don't think this is true at all. Probably the main reason people typecheck js with jsdoc annotations is because they don't want a TS compile step. Nodejs ES modules, and typescript, make it more complicated that it needs to be to write JS specifically for nodejs/cli scripts (not bundling to run in the browser). I now just prefer to // @ts-check my js files when writing node scripts.
- eropple 3y ago> Probably the main reason people typecheck js with jsdoc annotations is because they don't want a TS compile step. Right, I agree--because it's slow. Like AFAIK the tooling roundtrip is why Svelte/SvelteKit made the change. For normal projects, you don't have that same problem. > for nodejs/cli scripts I just use ts-node with the ESM loader. It's drop-in, for my purposes at least. What problems are you seeing?
- hliyan 3y agoIf history is an indication, any part of the JavaScript ecosystem that is not part of a formal standard, no matter how widely used or deeply integrated into the industry, is prone to getting replaced or obsoleted. There was a time when front-end development without jQuery was unthinkable. We now seem to be slowly coming out of any era when front-end development without reactive component based SPAs (React, Angular etc.) was unthinkable. I suspect that in the long run, compile-to-JS type systems will go the same way. Perhaps the alternative is a set of optional run-time validation capabilities built into ECMAScript (syntactic sugar over `typeof`?), whatever that may be.
- javier123454321 3y agoHonestly the TC39 is the best case scenario. https://tc39.es/proposal-type-annotations/ https://tc39.es/proposal-type-annotations/ Forget runtime type validation, TS is really for preventing bugs at development time as well as IDE integration.
- josephg 3y agoIf types were added to javascript it could also dramatically improve the performance of javascript virtual machines (V8 and friends). It seems so silly to me that I write typescript, then the types are erased from my code, sent to the browser, and then the browser tries to infer them as part of its own optimization process. Adding types to javascript proper would be fantastic.
- bazoom42 3y ago> If types were added to javascript it could also dramatically improve the performance of javascript virtual machines Are you sure? In my understanding modern JS engines basically makes type inference on the code anyway, and in the rare case when types are observed to change at runtime, it is special-cased. So static types might simplify this but I dont see how it would dramatically improve performance.
- 3y ago
- jerrygoyal 3y agothanks, but I'll ask "copilot for docs" to save time and get better answers.
- EugeneOZ 3y agoYou will not ask about things if you don't know they even exist.
- tough 3y agoQuite au contraire mon amie, you can just ask him to tell you typescript and keep prompting as you see fit. It will match your exact needs and style. There's also a beta copilot for docs which includes typescript https://githubnext.com/projects/copilot-for-docs https://githubnext.com/projects/copilot-for-docs
- nkzd 3y agoHow can you prompt copilot like chat? It only does autocomplete well.
- Kiro 3y agoYou need to have access to Copilot Chat, which is only available as an extension in VSC Insiders.
- fud101 3y agook great how do i read this on the train when i have no internet?
- smukherjee19 3y agoTake the README.md, print to pdf and transfer to your reading device before leaving?
- Uehreka 3y agoI think GP wants something akin to Beej’s guides, where he presents you with like 10 formats on the homepage and you pick the one that works best for your device and use case: https://beej.us/guide/bgnet/ https://beej.us/guide/bgnet/
- FireInsight 3y agogit clone
- weinzierl 3y agoI find TypeScript's type system fascinating, especially the intersection types. I wish there was an article contrasting TS's type system with that of other languages and just pointing out the unique to TS parts.
- okeuro49 3y agoI've found the biggest difference is structural vs. nominal types. https://www.typescriptlang.org/play#example/structural-typing https://www.typescriptlang.org/play#example/structural-typin... It seems particularly suited to dealing with random JSON payloads.
- mirekrusin 3y agoFlow supports both nominal types for classes with correct variance and structural types for everything else. This is correct model for js, flow is ahead of typescript in this case.
- za3faran 3y agoJava has intersection types.
- klysm 3y agoBut Java is nominally typed so I’d argue it’s not really the same
- MichaelMoser123 3y agomany thanks! Many backend jobs are suddenly requiring typescript knowledge, seems to be in high demand right now.
- ywhqj 3y ago[flagged]
- llamaLord 3y agoIf this is concise, I'd hate to see verbose...
- RichieAHB 3y agoOne thing I’ve never found a name for (and, as a result, a good way to reference to people) is Typescript’s interesting behaviour around the Erased Structural Types mentioned here: https://github.com/gibbok/typescript-book#erased-structural-types https://github.com/gibbok/typescript-book#erased-structural-.... Objects passed to a function that accepts an object type can generally have more properties that needed given Typescript’s structural typing. That is unless you pass a literal directly to the function (I.e. inline the obj variable in the linked example). In which case Typescript will complain about the excess properties. Ergonomic but confusing the first time you come across it.
- mirekrusin 3y agoThose are exact object types which have first class support in flow and no support with exception of literals like you mention in typescript. Flow is ahead in this case.
- chad_oliver 3y agoI believe this implements an exact object type in Typescript: type Exactly<Candidate, Shape> = Shape & { [key in keyof Candidate]: key extends keyof Shape ? Shape[key] : never; }; Credit goes to Manan Tank: https://twitter.com/MananTank_/status/1677610004743086080 https://twitter.com/MananTank_/status/1677610004743086080
- mirekrusin 3y agoNo, it doesn't. It can't be represented in TypeScript type system. You can't create utility type to have it. Have a look at Flow docs [0] if you want to learn more. His utility type works on diff between two provided types, this is something else. Exact object type is a kind of type which doesn't allow extra, undeclared properties to be used for it to be satisfied. You can't emulate it if it's not supported by type system. The whole thing is implemented way better in Flow than TypeScript, ie. spreads map to how runtime treats them, this is also not something TypeScript can represent and Flow supports. Also opaque types have first class support and many other things. [0] https://flow.org/en/docs/types/objects/#exact-and-inexact-object-types https://flow.org/en/docs/types/objects/#exact-and-inexact-ob...
- throwaway92921 3y agoGotta say I’ve tried using TS and frankly haven’t seen too much benefit. This makes me suspicious since I’m clearly in the minority about this. I find it mildly useful as a form of documentation - and the intellisense is nice, but otherwise find it cumbersome to use. In general - while I’ve experienced some issues with types in JS, it usually isn’t the root cause of most bugs I encounter.
- revskill 3y agoThe benefit is, your project needs a compiling phase in order to run. It's safer than just deploy and run your javascript code without that phase.
- toldyouso2022 3y agoEvery time I say "I'm not gonna beed typescript for something this small", something happens that makes me think typescript may have been better anyway.
- rpbiwer2 3y agoI agree, but I do wonder why the knee jerk reaction is "I'm not going to need it". The setup cost is basically zero! If TypeScript offers any benefit at all, why _not_ use it, since the benefits will come at near zero cost?
- irrational 3y ago> since the benefits will come at near zero cost? Maybe if you already have learned TS, otherwise there is a huge cost to learning it.
- rpbiwer2 3y agoSure but my gripe is with those who already use TS
- rpbiwer2 3y ago
- yanis_t 3y agoMany people fail to see that the real benefit of using TypeScript is better tooling (code completion, intenseness, refactoring capabilities, self-documenting code) and not the mere ability to catch some stupid runtime errors at compile time.
- hliyan 3y agoThis is one of the reasons I find it hard to like TypeScript. Despite proclamations about reducing errors and maintainability, I have seen many developers largely motivated to introduce TypeScript for no reason other than the ability to hit "." and see what comes next. While that is a good goal, transmogrifying a type system (and a rather weak one at that) into an IntelliSense annotation mechanism seems excessive.
- tmountain 3y agoWhy do you think the type system is weak? I was using PureScript (Haskell derivative) prior to TypeScript, and I have found its typing capabilities to be very impressive.
- IshKebab 3y agoDo you have an alternative suggestion? Because the ability to type "." and see what comes next, go-to-definition, find all references, rename symbol, etc. are all enormous time savers. Just as yanis_t said, they're easily worth it even if you ignore the significant bug reduction (approximately 15%). I don't know of any other way to get that kind of productivity boost. What would you suggest instead?
- gardenhedge 3y agoTypescript errors are the worst errors give ever seen
- rcfox 3y agoYou've obviously never done any C++ involving template metaprogramming.
- justinhj 3y agoVery timely. I've been using some inherited Typescript on a project for a while and was looking for exactly this kind of concise laying out of the language that you can read in an hour or two.