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

推荐订阅源

D
DataBreaches.Net
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Webroot Blog
Webroot Blog
AI
AI
P
Palo Alto Networks Blog
Attack and Defense Labs
Attack and Defense Labs
WordPress大学
WordPress大学
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Spread Privacy
Spread Privacy
T
Tor Project blog
罗磊的独立博客
小众软件
小众软件
S
Security Affairs
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Apple Machine Learning Research
Apple Machine Learning Research
T
Threatpost
NISL@THU
NISL@THU
博客园_首页
PCI Perspectives
PCI Perspectives
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
N
News and Events Feed by Topic
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Forbes - Security
Forbes - Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
T
Threat Research - Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Security Latest
Security Latest
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
A
About on SuperTechFans
爱范儿
爱范儿
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
TaoSecurity Blog
TaoSecurity Blog
宝玉的分享
宝玉的分享
G
GRAHAM CLULEY
雷峰网
雷峰网
F
Full Disclosure
I
Intezer
Cloudbric
Cloudbric
博客园 - 三生石上(FineUI控件)
U
Unit 42

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 5 Inconvenient Truths about 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 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
TypeScript: In defense of any
2021-09-14 · via oida.dev | TypeScript, Rust

A couple of days ago I found this beauty on the internet: Give up and use any. That’s a fantastic website, absolutely hilarious! It also shows how much TypeScript has grown in popularity. You have to have a certain reach to get critics to create websites like this.

Let’s put the fact that there’s a website aside for a moment and talk about its message: Use any. There has been quite a movement in the past that folks absolutely shouldn’t use any. Even the TypeScript handbook says so in its Do’s and Dont’s. In my book, there’s one lesson of 50 dedicated to the problem with any and that it’s wise to avoid it. But here’s a twist, and this might be controversial to some of you: I think using any in TypeScript is absolutely ok.

JavaScript’s default #

Ok, before you start screaming at your screen and open your Twitter client to shout at me, hear me out. TypeScript is a gradual type system. The possibility to turn on typings as much as you like is part of its design. I also think this is one of the reasons TypeScript is so immensely popular.

any is JavaScript’s default. JavaScript has types, but there is no type-checking. any is TypeScript’s way of saying: Turn off type-checking for me. And this can be helpful in some scenarios. For example:

Migrating from JavaScript. #

When you go from JavaScript to TypeScript, chances are that you already have a large codebase with a lot of implicit information on how your data structures and objects work. It might be a chore to get everything spelled out in one go. any can help you migrate to a safer codebase incrementally.

Using third party libraries with poor type support #

You might have one or the other JavaScript dependency that still refuses to use TypeScript (or something similar). Or even worse: There are no up-to-date types for it. Definitely Typed is a great resource, but it’s also maintained by volunteers. It’s a formalization of something that exists in JavaScript but is not directly derived from it. There might be errors (even in such popular type definitions like React’s), or they just might not be up to date!

This is where any can help you greatly. When you know how the library works, if the documentation is good enough to get you going, if you use it sparingly, any can be a relief instead of fighting types.

Smooth criminal development #

In the last couple of articles I wrote about why some things work different in TypeScript than in JavaScript. This has a lot to do with trade-offs the TypeScript team has to make to ensure type safety for edge cases. Yes, in your current scenario, JavaScript would work like that. But there are scenarios where your software might horrendously break.

In cases like this, any can help you to switch off type checking for a moment because you know what you’re doing. And since you can go from every type to any, but also back to every other type, you have little, explicit unsafe blocks throughout your code where you are in charge of what’s happening. This is what I call Smooth criminal development.

Know what it does #

Btw. here is what I had to say in TypeScript in 50 Lessons: any is a wildcard! Use any and you can go all out and forget about any type-checking at all. So why does something like any even exist?

This is due to the nature of JavaScript. In JavaScript, you are not bound to a type, and values from any type can appear in your variables and properties. Some developers make excessive use of that!

any reflects JavaScript’s overarching flexibility; you can see it as a backdoor to a world where you want neither tooling nor type safety. By all means, use any, but understand how it works and what to do with it – use it at your own risk!

Be certain that you want to use any explicitly as a type annotation. And if you want to enter through the backdoor to JavaScript flexibility, be very intentional through a type assertion:

// theObject is an object we don’t have a type for,
// but we know exactly what
// we are doing!
(theObject as any).firstLetter.toUppercase()

Of course, type assertions also work with other types. If you want to make sure you don’t have any somewhere in your code you don’t expect it to be, set the compiler flag noImplicitAny to true. TypeScript will then make sure that you either assign values to correctly infer types or, in the case of any, make sure that you explicitly annotate.

// deliveryAddress is of type any, because we
// didn’t annotate a specific type. Implicit anys are
// hard to track down later on, that’s why it’s good
// to have TypeScript scream at us
function printAddress(deliveryAddress) {
console.log(deliveryAddress)
}

If we annotate function parameters and variables explicitly with any, they become easier to track down later on once we have decided on the real types.

Consider unknown #

any has a counterpart called unknown. It’s a top type as well, encompassing all possible values. The usage is different, though. Where any allows us to do everything, unknown doesn’t allow us to do anything. This can work in a lot of scenarios where you don’t know which type you’re dealing with (e.g. passing around arbitrary data that can’t be formalized in a generic variable).

function selectDeliveryAddress(addressOrIndex: unknown): string {
if(typeof addressOrIndex === 'number' &&
addressOrIndex < deliveryAddresses.length) {
return deliveryAddresses[addressOrIndex]
}
return addressOrIndex; //💥 Error!
}

Boom! This is exactly what we want: “Type number is not assignable to type string.” We must do type checks and trigger control flow analysis; otherwise, TypeScript will throw an error.

Bottom line #

So here’s the bottom line: any is ok if used carefully. Turn on noImplicitAny, so you have to annotate every usage of it. Being explicit is key.

Related Articles