サクサク読めて、アプリ限定の機能も多数!
トップへ戻る
やる気の出し方
2ality.com
This blog post gives the bird’s eye view of how TypeScript works: What is the structure of a typical TypeScript project? What is compiled and how? How can we use IDEs to write TypeScript? This post is meant to be read before learning how to write TypeScript code (material for doing that is listed at the end). The structure of TypeScript projects # This is one possible file structure for TypeScrip
In this blog post, we examine how Arrays can be typed in TypeScript. Roles of Arrays # Arrays are used in the following two roles in JavaScript (and sometimes a mix of the two): Lists: All elements have the same type. The length of the Array varies. Tuple: The length of the Array is fixed. The elements do not necessarily have the same type. TypeScript accommodates these two roles by offering vari
What are types in TypeScript? This blog post describes two perspectives that help with understanding them. Three questions for each perspective # The following three questions are important for understanding how types work and need to be answered from each of the two perspectives. What does it mean for myVariable to have the type MyType? let myVariable: MyType = /*...*/; Is SourceType assignable
A recent blog post explored how TypeScript enums work. In this blog post, we take a look at alternatives to enums. Unions of singleton values # An alternative to creating an enum that maps keys to values, is to create a union of singleton types (one per value). Read on to see how that works. Unions of string literal types # Let’s start with an enum and convert it to a union of string literal typ
In this blog post, we examine a pattern for implementing enums in JavaScript that is based on classes. We’ll also take a look at Enumify, a library that helps with the enum pattern. Implementing enums: first attempts # An enum is a type that consists of a set of values. For example, TypeScript has built-in enums and with them, we can define our own boolean type: enum MyBoolean { false, true, } Or
Update 2020-04-02: Today, the ES2020 candidate was released, with the final feature set of that version. This blog post describes what’s new. A word on ECMAScript versions # Note that since the TC39 process was instituted, the importance of ECMAScript versions has much decreased. What really matters now is what stage a proposed feature is in: Once it has reached stage 4, it can be used safely. Bu
Warning: This blog post is outdated. It may contain incorrect information. Update 2019-11-22: ESM support in Node.js is not experimental, anymore. This post was updated to reflect that. Conditional exports are now explained. In this blog post, we look at npm packages that contain both ES modules and CommonJS modules. Required knowledge: How ES modules are supported natively on Node.js. You can rea
The import() operator lets us dynamically load ECMAScript modules. But they can also be used to evaluate JavaScript code (as Andrea Giammarchi recently pointed out to me), as an alternative to eval(). This blog post explains how that works. eval() does not support export and import # A significant limitation of eval() is that it doesn’t support module syntax such as export and import. If we use i
Warning: This blog post is outdated. Instead, read section “globalThis” in “JavaScript for impatient programmers”. The ECMAScript proposal “globalThis” by Jordan Harband provides a new standard way of accessing the global object. JavaScript’s global scope # JavaScript’s variable scopes are nested and form a tree whose root is the global scope. That scope is only the direct scope when a script run
The following sections describe the behavior of some of these constructs in more detail. const and let: temporal dead zone # For JavaScript, TC39 needed to decide what happens if you access a constant in its direct scope, before its declaration: { console.log(x); // What happens here? const x; } Some possible approaches are: The name is resolved in the scope surrounding the current scope. You get
Follow-up blog post: “Hybrid npm packages (ESM and CommonJS)” Node.js 12 (which was released on 2019-04-23) brings improved support for ECMAScript modules. It implements phase 2 of the plan that was released late last year. For now, this support is available behind the usual flag --experimental-modules. Read on to find out how exactly this new support for ECMAScript modules works. Brief spoiler: T
In recent years, JavaScript has grown considerably in size. This blog post explores what’s still missing. Notes: I’m only listing the missing features that I find most important. Many others are useful, but there is also a risk of adding too much. My choices are subjective. Almost everything mentioned in this blog post is on TC39’s radar. That is, it also serves as a preview of a possible future J
Status of ECMAScript module (ESM) support in Node.js: Experimental support for ESM was added in Node.js 8.5.0 (2017-09-12). Afterwards, the Node.js Foundation Technical Steering Committee formed the Modules Team to help design missing pieces and to guide the implementation towards its (non-experimental) release. The Modules Team is comprised of people from several different web development areas (
Update 2019-01-29: The feature set of ECMAScript 2019 is now final (source) and described in this blog post. A word on ECMAScript versions # Note that since the TC39 process was instituted, the importance of ECMAScript versions has much decreased. What really matters now is what stage a proposed feature is in: Once it has reached stage 4, it can be used safely. But even then, you still have to ch
In this blog post, we run shell commands as child processes in Node.js. We then use async language features to read the stdouts of those processes and write to their stdins. Running commands in child processes # Let’s start with running a shell command in a child process: const {onExit} = require('@rauschma/stringio'); const {spawn} = require('child_process'); async function main() { const filePa
Warning: This blog post is outdated. Instead, read chapter “The essentials of TypeScript” in “Tackling TypeScript”. This blog post is a quick introduction to TypeScript’s notation for static types. What you’ll learn # After reading this post, you should be able to understand what the following code means: interface Array<T> { concat(...items: Array<T[] | T>): T[]; reduce<U>( callback: (state: U,
Update 2018-04-24: Using the library stringio to read lines from stdin. Node.js 10 was released on April 24, 2018. This version provides experimental support for asynchronously iterating over readable streams. This blog post explains how that works. Reading streams asynchronously # In this section, we examine two ways of reading data from a stream asynchronously: via callbacks and via asynchronou
In this blog post, I describe the pros and cons of three programming languages/dialects: JavaScript, TypeScript and ReasonML. My descriptions are based on recent experiences with TypeScript and ReasonML on a few smaller real-world projects and on years of experience with JavaScript. Before we take a look at the languages, let’s first consider the pros and cons of static typing, given that TypeScri
The proposal “String.prototype.matchAll” by Jordan Harband is currently at stage 3. This blog post explains how it works. Before we look at the proposal, let’s review the status quo. Getting all matches for a regular expression # At the moment, there are several ways in which you can get all matches for a given regular expression. RegExp.prototype.exec() with /g # If a regular expression has the
JavaScript never removes old features – new versions are always backward compatible. I wish we could clean it up, but staying compatible has too many benefits, as I’ll explain in this blog post. The benefits of backward compatibility # These are benefits of staying backward compatible: It is trivially easy to migrate to new language versions. For example, all old code was already compatible with
The proposal “Numeric Separators” by Sam Goto and Rick Waldron lets us use _ as a separator in numeric literals. This blog post explains how that works. What are numeric literals? # JavaScript has several numeric literals: Decimal literals: 123.45, 86, 12e-5 Binary integer literals: 0b1011 Octal integer literals: 0o765 Hexadecimal integer literals: 0x2FF Interestingly, unary minus (-) is an opera
In this blog post, I take a different approach to explaining this in JavaScript: I pretend that arrow functions are the real functions and ordinary functions a special construct for methods. I think it makes this easier to understand – give it a try. Two kinds of functions # In this post, we focus on two different kinds of functions: Ordinary functions: function () {} Arrow functions: () => {} Or
[2018-02-08] Warning: outdated content. Read the book “Exploring ReasonML”, instead (free online). This blog post gives a brief high-level explanation of Facebook’s new programming language, ReasonML. Blog posts in this series # Background: What is ReasonML [this blog post] Getting started with ReasonML What is planned for ReasonML? Core language: Basic values and types let bindings and scopes Pa
Update 2017-11-18a: I worded a few things more carefully, to make it clear that I don’t hate functional programming (I’m a fan). Update 2017-11-18b: I added subsections to Sect. “Currying is in conflict with some of JavaScript’s foundations”. In this blog post I explain why, in my opinion, currying is in conflict with some of JavaScript’s foundations. Recommended reading if you are not sure what c
The proposal “import.meta” by Domenic Denicola is currently at stage 3. This blog post explains how it works. Module metadata # When it comes to working with a module, you are normally interested in what’s inside it. But occasionally, you need information about the module – its metadata. One example of module metadata for Node.js modules is the global variable __dirname which contains the path of
Update 2018-12-20: Warning: This blog post is outdated! Consult “ECMAScript modules in Node.js: the new plan” for the latest information. Update 2017-09-14: Major rewrite of Sect. “Checklist: things to watch out for”. New FAQ entries. Starting with version 8.5.0, Node.js supports ES modules natively, behind a command line option. Most of the credit for this new functionality goes to Bradley Farias
Beyond typeof and instanceof: simplifying dynamic type checks This blog post describes a technique for making instanceof applicable to more values (on the right-hand side). Specifically, for primitive values. Background: typeof vs. instanceof # In JavaScript, you have to choose when it comes to checking the type of a value. The rough rule of thumb is: typeof checks if a value is an element of a p
The proposal “Optional catch binding” by Michael Ficarra is at stage 4 and therefore part of ECMAScript 2019. This blog post explains how it works. Overview # The proposal allows you to do the following: try { ··· } catch { ··· } That is useful whenever you don’t need the binding (“parameter”) of the catch clause: try { ··· } catch (error) { ··· } If you never use the variable error, you may as w
Update 2017-08-12: I added an FAQ. The proposal “Promise.try()” by Jordan Harband is currently at stage 1. This blog post explains how it works. Promise.try() # To understand what Promise.try() does, consider the following code: function countPlusOneAsync() { return countAsync() .then(result => { return result + 1; }); } If countAsync() throws a synchronous exception then so does countPlusOneAsyn
次のページ
このページを最初にブックマークしてみませんか?
『2ality – JavaScript and more』の新着エントリーを見る
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く