1600636500
TypeScript code around the world is littered with the any
type, and it drives me absolutely crazy. There is no (valid) reason to use it in application code, and you are opting out of the safety of a static type system.
Many developers who are new (or not so new to TypeScript) assume that when you don’t know what type you are going to get, you should type it with any
. TypeScript’s official documentation also tells you that this is not the case:
“Don’t use
_any_
as a type unless you are in the process of migrating a JavaScript project to TypeScript. The compiler effectively treats_any_
as “please turn off type checking for this thing”. It is similar to putting an_@ts-ignore_
comment around every usage of the variable. This can be very helpful when you are first migrating a JavaScript project to TypeScript as you can set the type for stuff you haven’t migrated yet as_any_
, but in a full TypeScript project you are disabling type checking for any parts of your program that use it.”
Unless you are adding type definitions to an open source JavaScript library, do not use any
. If you are maintaining an open source JS library, then I appreciate you adding type definitions at all and understand the time constraints you are under. If throwing in an any
here and there allows you to add types to 90% of your API, then I think this is a valid use case. I hope they are all refactored to unknown
one day when time permits. Ideally, members of the TypeScript community could make PRs and help refactor these for you.
What if I want to accept anything or don’t know the type I’ll get? Again, TypeScript’s documentation comes to the rescue:
“In cases where you don’t know what type you want to accept, or when you want to accept anything because you will be blindly passing it through without interacting with it, you can use
[_unknown_](https://www.typescriptlang.org/play/#example/unknown-and-never)
.”
#typescript #nodejs #javascript #programming #software-development
1654588030
TypeScript Deep Dive
I've been looking at the issues that turn up commonly when people start using TypeScript. This is based on the lessons from Stack Overflow / DefinitelyTyped and general engagement with the TypeScript community. You can follow for updates and don't forget to ★ on GitHub 🌹
If you are here to read the book online get started.
Book is completely free so you can copy paste whatever you want without requiring permission. If you have a translation you want me to link here. Send a PR.
You can also download one of the Epub, Mobi, or PDF formats from the actions tab by clicking on the latest build run. You will find the files in the artifacts section.
All the amazing contributors 🌹
Share URL: https://basarat.gitbook.io/typescript/
Author: Basarat
Source Code: https://github.com/basarat/typescript-book/
License: View license
1601549700
Today I am going to talk about new features in Typescript 4.0.
TypeScript 4.0 comes with lots of new features to make JavaScript development easier.
You can label tuple elements.
You can write:
type Range = [start: number, end: number];
to restrict args
to have a string and a number.
you can also write:
type Foo = [first: number, second?: string, ...rest: any[]];
to have rest entries in your tuple.
If your tuple has type Foo
, then the tuple starts with a number and a string.
Then the rest of the entries can be anything.
Labels don’t require you to name your variables differently when destructuring.
For example, if you have:
function foo(x: [first: string, second: number]) {
const [a, b] = x;
}
then you can name the destructured variables anything you want.
#software-development #typescript-with-react #typescript #typescript-4 #react native
1599308024
icrosoft recently announced the availability of TypeScript version 4.0. The developers at the tech giant claimed that this version of the language represents the next generation of TypeScript with more expressivity, productivity as well as scalability.
Developed by the tech giant, TypeScript is an open-source programming language that is built on top of JavaScript by adding syntax for static type definitions. The types in this language provide a way to describe the shape of an object, providing better documentation as well as allowing TypeScript to validate that the code is working correctly.
According to the latest Stack Overflow Developers survey 2020, it secured the second position as the most loved language and 9th position among 25 programming languages as the most commonly used programming language by the developers. In one of our articles, we discussed how TypeScript weighs over other programming languages.
It is one of the fastest-growing programming languages among the developers. The motive behind this language is that while writing down the types of values and where they are used, developers can use TypeScript to type-check the code and let them know about mistakes before they run the code. TypeScript compiler can be used to strip away types from the code, leaving them with clean, readable JavaScript that runs anywhere.
In the present scenario, TypeScript is a core part of many developer’s JavaScript stack. The language adds optional types to JavaScript that support tools for large-scale JavaScript applications for any browser, for any host and on any operating systems.
The program manager of TypeScript, Daniel Rosenwasser, said in a blog post, “In our past two major versions, we looked back at some highlights that shined over the years. For TypeScript 4.0, we’re going to keep up that tradition.”
Based on the feedback by the developer’s community, TypeScript 4.0 includes many intuitive features that are focussed on boosting the performance of this language. Some of them are mentioned below-
According to Rosenwasser, previously, compiling a program after a previous compile with errors under incremental would result in extremely slow performance when using the –noEmitOnError flag. The reason is, none of the information from the last compilation would be cached in a .tsbuildinfo file based on the –noEmitOnError flag.
But now TypeScript 4.0 changes this. The new update provides a great speed boost in these scenarios, and in turn, improves the build mode scenarios, which imply both –incremental and –noEmitOnError.
#developers corner #microsoft #microsoft releases typescript 4.0 #programming language #programming language with high salary #typescript #typescript 4.0
1603953021
Maybe you’ve been using JavaScript for a while and you’ve been hearing the buzz around… TypeScript. In this 2 part series, we’ll take a look at Why TypeScript? and Why Not TypeScript?
According to the TypeScript website “TypeScript extends JavaScript by adding Types… TypeScript code is transformed into JavaScript code via the TypeScript compiler or Babel. This JavaScript is clean, simple code which runs anywhere JavaScript runs…”. In short, many refer to TypeScript as a superset of JavaScript.
In my opinion, this is kind of true and kind of not. Technically, not all valid JavaScript code can also be TypeScript code. We’ll talk about that later on. But, at its core, TypeScript was designed to allow developers to turn JavaScript, a dynamic programming language (one that is interpreted at runtime) into a statically typed programming language (one that is compiled before running). Let’s dive in.
TypeScript makes use of features similar to languages like C## or Java: it truly is object oriented. This allows for more robust code. It enables the developer to implement type checking and make use of interfaces, classes, inheritance, etc. This can up the game as far as clean code when looking at a JavaScript program and a TypeScript program.
_Why did you throw in the word _potentially? Well, most valid JavaScript code can become TypeScript code. This means you don’t have to use the object oriented or typed features of TypeScript. Remember, TypeScript is optionally typed. It is not required. If TypeScript is utilized merely like JavaScript then improved code quality may not actually be a thing.
#typescript #programming #javascript #web-development #developer
1604042121
Write Safer JavaScript Applications With TypeScript!
Learn how TypeScript can help you build safer and more resilient JavaScript applications. Expect some demos, some code and a lot of fun!
#typescript #typescript #programming #web-development #developer