惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
L
LINUX DO - 最新话题
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Know Your Adversary
Know Your Adversary
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
Lohrmann on Cybersecurity
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy & Cybersecurity Law Blog
K
Kaspersky official blog
The Last Watchdog
The Last Watchdog
Webroot Blog
Webroot Blog
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
WordPress大学
WordPress大学
L
LINUX DO - 热门话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Visual Studio Blog
O
OpenAI News
AI
AI
Hacker News: Ask HN
Hacker News: Ask HN
V2EX - 技术
V2EX - 技术
GbyAI
GbyAI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Simon Willison's Weblog
Simon Willison's Weblog
S
Schneier on Security
Spread Privacy
Spread Privacy
Y
Y Combinator Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
F
Fortinet All Blogs
C
CERT Recently Published Vulnerability Notes
T
The Blog of Author Tim Ferriss
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
N
News and Events Feed by Topic
Project Zero
Project Zero
小众软件
小众软件
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
B
Blog
G
Google Developers Blog

oida.dev | TypeScript, Rust

TypeScript's `erasableSyntaxOnly` Flag Unsafe for work Tokio: Macros Tokio: Channels Tokio: Getting Started Network Applications on the Tokio Stack Remake, Remodel, Reduce. The `never` type and error handling in TypeScript Refactoring in Rust: Introducing Traits Refactoring in Rust: Abstraction with the Newtype Pattern Announcing the TypeScript Cookbook TypeScript: Iterating over objects The road to universal JavaScript 10 years of oida.dev Rust: Tiny little traits The TypeScript converging point How not to learn TypeScript Getting started with Rust Introducing Slides and Coverage TypeScript: The humble function overload TypeScript + React: Children types are broken TypeScript: In defense of any Rust: Enums to wrap multiple errors Dissecting Deno Error handling in Rust TypeScript: Unexpected intersections Upgrading Node.js dependencies after a yarn audit TypeScript: Array.includes on narrow types TypeScript + React: Typing Generic forwardRefs shared, util, core: Schroedinger's module names Learning Rust and Go TypeScript: Narrow types in catch clauses TypeScript: Low maintenance types Tidy TypeScript: Name your generics Tidy TypeScript: Avoid traditional OOP patterns Tidy TypeScript: Prefer type aliases over interfaces Tidy TypeScript: Prefer union types over enums My new book: TypeScript in 50 Lessons Go Preact! ❤️ this in JavaScript and TypeScript TypeScript and ECMAScript Modules TypeScript + React: Why I don't use React.FC TypeScript + React: Component patterns TypeScript: Augmenting global and lib.dom.d.ts Vite with Preact and TypeScript TypeScript: Union to intersection type 11ty: Generate Twitter cards automatically Are large node module dependencies an issue? TypeScript: Variadic Tuple Types Preview TypeScript: Improving Object.keys Remake, Remodel. Part 4. TypeScript + React: Typing custom hooks with tuple types TypeScript: Assertion signatures and Object.defineProperty TypeScript: Check for object properties and narrow down type Boolean in JavaScript and TypeScript void in JavaScript and TypeScript Symbols in JavaScript and TypeScript Why I use TypeScript TypeScript + React: Extending JSX Elements TypeScript: Validate mapped types and const context TypeScript: Match the exact object shape TypeScript: The constructor interface pattern Streaming your Meetup - Part 4: Directing and Streaming with OBS Streaming your Meetup - Part 3: Speaker audio Streaming your Meetup - Part 2: Speaker video Streaming your Meetup - Part 1: Basics and Projector TypeScript and React Guide: Added a new styles chapter TypeScript and React Guide: Added a new render props chapter TypeScript and React: Styles and CSS TypeScript and React TypeScript and React Guide: Added a new prop types chapter TypeScript without TypeScript -- JSDoc superpowers TypeScript: Mapped types for type maps JAMStack vs serverless web apps The Unsung Benefits of JAMStack Sites TypeScript: Ambient modules for Webpack loaders My most favourite talks in 2018 TypeScript and React Guide: Added a new context chapter TypeScript: Built-in generic types TypeScript: Type predicates JSX is syntactic sugar TypeScript and React Guide: Added a new hooks chapter Getting your CfP application right FAQ on our Angular Connect Talk: Automating UI development TypeScript and Substitutability Debugging Node.js apps in TypeScript with Visual Studio Code From Medium: Deconfusing Pre- and Post-processing From Medium: PostCSS misconceptions Saving and scraping a website with Puppeteer Cutting the mustard - 2018 edition Wordpress as CMS for your JAMStack sites My most favourite podcast episodes in 2017 My most favourite talks in 2017 My most favourite books in 2017 The Best Request Is No Request, Revisited Not so hidden figures - Organizing ScriptConf My podcast journey to ScriptCast Grid layout, grid layout everywhere! #scriptconf and #devone Object streams in Node.js
5 Inconvenient Truths about TypeScript
2023-07-17 · via oida.dev | TypeScript, Rust

I’m writing books about TypeScript and I do workshops and trainings online and in-house. Every time I meet a new group of developers there are some TypeScript facts that they need to be confronted with:

1. TypeScript won’t save you from JavaScript #

Every time I give TypeScript workshops there is at least one person learning TypeScript because they tried JavaScript and didn’t work out for them. TypeScript should be better, right? The misconception that TypeScript is a “better” or “fixed” version of JavaScript is very common, it has been around since the very early days when TypeScript was heavily promoted by developers who usually did C#/.NET and had a need to write client-side APIs as well. This was also my first contact with TypeScript: Somebody told me that they found a “better version”. And this was also the reason why I originally rejected TypeScript.

But let me tell you that TypeScript is without a doubt a superset of JavaScript, meaning that it takes all the history, quirks, and gotchas from JavaScript along. Yes, it helps you avoid mistakes, and it helps you work with a dynamically typed programming language. It errors when something doesn’t add up. But in its core, it’s still JavaScript. If you work long enough with TypeScript, you see the yellow patches shine through the blue coating of static types.

TypeScript’s type system fully embraces structural typing – for good reason – and this, combined with the fact that types are just meta information that lie on top of existing code can be alienating to a lot of people who expect a quick transition from one language to the other.

If you want to get into TypeScript, don’t think you can leave JavaScript behind. It will find you, and it will get you.

2. TypeScript adds complexity #

TypeScript is known for its gradual approach: Use as many types as you like, go as deep as you want. TypeScript also wants to continually lower the barrier to entry, making it sure that people who know just as much JavaScript to be dangerous can benefit from type information. Every time you write JavaScript in e.g. Visual Studio Code, TypeScript runs behind the curtains and gives you information on built-in APIs, etc. Very subtle, and sometimes without anybody noticing. In fact, a lot of people think that they can live without TypeScript because JavaScript support is so fantastic in modern editors. Guess what’s, it has always been TypeScript!

Still, TypeScript adds a layer of complexity to your project that you need to be aware of. And I’m not only talking about types and the complex, advanced features like Conditional Typing. I’m talking about all the knobs, switches, and integrations that TypeScript brings to the table to be compatible with fricking everything! Running TypeScript in Deno? Check. Node? Of course! Internet Explorer 5? We got you. With Babel? Sure! Babel and Webpack? Yes. Esbuild? You name it! With my seven-year-old charting library? Turn off noImplicitAny, then it’ll work! A mix between UMD, AMD, and CommonJS? Hah, sure! JSX? Yup. TSX? All clear! TSX in React 18? Uh-huh! 17? It does!

You get the idea. The TypeScript compiler is configured through TSConfig, a huge JSON file with lots of possibilities to tailor TypeScript to your needs. There are so many different configuration options that it gets really hard to tell which rules are now actually applied. And once you overcome this configuration extravaganza, you can start looking into all the trade-offs TypeScript is doing for you so you get safe code.

TypeScript is a complex matter.

3. TypeScript is not type-safe #

This might be a surprise for many, but TypeScript actually isn’t type-safe. TypeScript’s type-checking works really well in what I like to call the “inner type world” of your application. That part where you defined your types, the operations on them, and their flow throughout your program. Here, everything that should work together will work together, everything that doesn’t will cause red squiggly lines and compiler errors.

But there’s stuff at the edges. If you leave the inner world of type safety and need to reach out for basically any IO. User input. File access. Fetching data over the internet. Here TypeScript has no clue what the types are about and needs to rely on what you tell it. You need to make type annotations, type assertions, or huge control flow checks to give TypeScript a hint of what to expect. And those measurements are just as good as you are. in TypeScript, you have the ability to change a type to something else at any time. You can override from a broad type that allows for many values but is less specific to a narrow type that allows for fewer values but gives more information at any time, sometimes without even noticing. And there are situations, especially with APIs that return any, where you might annotate something entirely different than you get back.

And nothing keeps you from it.

Not only that, but TypeScript has a few corner cases where it trades type safety for productivity. And those are the scenarios that can really bite you!

The fun thing about is that TypeScript always had a strict non-goal to be provably type-safe. It’s all about making developers productive. It should not get in the way, but help you find the right information fast. Another trade-off.

4. TypeScript comes in many flavors #

What TypeScript are you writing? JavaScript with a dash of types? Going all-in with class hierarchies and using all language extensions like it’s 2012? Writing mostly React and needing the TSX extension? Do you write functional code and work a lot with conditional types and variadic tuple types? Or do you use experimental decorators that provide information for a gazillion other compilers that run afterward? What works best for you? And especially for you and your team?

TypeScript has so many possibilities that I haven’t seen a single project that looks similar to another one. Every team figures out something different and uses a variety of tools to express themselves in TypeScript. And they all come with different features and trade-offs. Enums might have nice APIs but they are nominal in a structural type system and produce non-treeshakeable code. Abstract classes help you define shared behavior but have no roots in JavaScript at all. Conditional types almost always require additional documentation so people can understand what has been done after some passage of time.

Have you decided on whether to use types or interfaces? Const enums or regular enums? Do you infer or annotate? Do you care? What’s your TypeScript flavor?

It’s almost like in C++, where teams always need to decide on a subset of the language to manage complexity. TypeScript asks the same from you.

5. It’s still worth it #

I’ve been doing TypeScript for 6 years and I have written two books on that topic. I do workshops in-house for teams and publicly through organizations like Smashing Magazine. Today, I showed you a lot of examples where TypeScript is not all glitter and glory.

Still. TypeScript is totally worth it. Understand the language, understand its trade-offs and caveats, and understand what it brings to your table. Then you can fully embrace the power of TypeScript as a type system and programming language.

And you know what, then it becomes fun!

TypeScript is here to make your life and the life of your team easier. And it succeeds! Try going back to a project after 6 months of time and try to recreate the mental model you had from your application. Or look at well-crafted types that tell you a story. I’d always go for TypeScript.

Related Articles