iTranslated by AI
Exploring the Limits of Type Inference in Biome v2
Hello everyone. Recently, Biome v2 was released and became a hot topic. One of the new features in Biome v2 is type inference.
Type-aware linting for TypeScript code has so far been provided by TypeScript-ESLint. This has the disadvantage of being heavy because it uses the actual TypeScript compiler to obtain type information. While TypeScript itself is working on performance improvements through things like porting to Go, Biome has taken a different approach to this problem. That is, performing type inference independently without relying on the official TypeScript compiler.
However, the TypeScript compiler is a very complex system, and it is virtually impossible to perfectly reproduce its type inference results with a different implementation. Therefore, Biome's type inference cannot completely replicate the behavior of the TypeScript compiler. In the Biome v2 release notes, using the noFloatingPromises rule as an example, it is stated that it can detect about 75% of cases compared to using the actual TypeScript compiler.
So, in this article, I will try out the type inference feature of Biome v2 and investigate how much type inference it can perform compared to the real TypeScript compiler.
A Simple Example
This time, we will investigate using the noFloatingPromises rule. This is a rule that detects code where a Promise is not awaited. Let's start with a simple example.
async function foo() {
return 3.14;
}
export async function main() {
foo();
}
In this code, while the foo function returns a Promise, the main function does not await the result. Therefore, a lint error is expected to occur.
When running biome lint against this, the following error is output:
index.ts:6:3 lint/nursery/noFloatingPromises FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ A "floating" Promise was found, meaning it is not properly handled and could lead to ignored errors or unexpected behavior.
5 │ export async function main() {
0 6 │ foo();
│ ^^^^^^
7 │ }
ℹ This happens when a Promise is not awaited, lacks a `.catch` or `.then` rejection handler, or is not explicitly ignored using the `void` operator.
ℹ Unsafe fix: Add await operator.
6 │ ··await·foo();
│ ++++++
It detects that the return value of foo() is a Promise and points out that await is necessary. Although there are no explicit type annotations for foo(), Biome infers the return value of the function and determines it is a Promise.
From here, let's gradually make the inference more difficult.
Trying new Promise
In the example above, since foo was defined as an async function, it was very obvious that the return value was a Promise. Next, let's try using new Promise.
function foo() {
return new Promise<number>((resolve) => {
setTimeout(() => {
resolve(3.14);
});
});
}
export async function main() {
foo();
}
In this example, the return value of the function foo is still a Promise. However, it uses new Promise without using the async keyword.
When running biome lint against this code, no lint error was detected.
Unfortunately, it seems that in this case, Biome cannot recognize that the return value of the function is a Promise. Since there is no type annotation for the return value of foo, to determine the type of foo, it would be necessary to look for the return statement inside foo, verify that its value is new Promise, and then determine that the type of this expression is a Promise. However, it seems that Biome is unable to perform this kind of type inference.
Explicitly Providing Type Annotations
Now, let's try adding a type annotation to the return value of the function.
function foo(): Promise<number> {
return new Promise<number>((resolve) => {
setTimeout(() => {
resolve(3.14);
});
});
}
export async function main() {
foo();
}
With this, Biome was able to detect the lint error.
I haven't looked at the internal implementation of Biome's type inference, but it likely doesn't inspect the function's body to determine its return type. To leverage Biome's type inference, it becomes important to explicitly specify the return type of the function.
Using Promises
Now, let's revert the function to an async function for clarity and try various scenarios.
async function foo(): Promise<number> {
return 3.14;
}
export async function main() {
console.log(foo());
}
When I ran biome lint on this example, surprisingly, no error was detected. I was a bit taken aback.
However, the official documentation states the following regarding the behavior of the noFloatingPromises rule:
This rule will report Promise-valued statements that are not treated in one of the following ways:
- Calling its
.then()method with two arguments- Calling its
.catch()method with one argumentawaiting itreturning itvoiding it
Since it refers to "Promise-valued statements", it can be interpreted that using a Promise within an expression is excluded, and detection is limited to cases where a Promise is left completely unhandled, such as foo();.
Perhaps because of this, no lint error was detected even when the Promise was assigned to a variable, as shown below:
async function foo(): Promise<number> {
return 3.14;
}
export async function main() {
const p = foo();
console.log(p);
}
Trying through an object
I also tried the case of an object method instead of a function.
const obj = {
foo: async () => {
return 3.14;
},
}
export async function main() {
obj.foo();
}
In this case, a lint error was detected. It seems that through some form of inference, it understood that obj.foo is a function that returns a Promise.
By the way, the lint error disappears if you do the following (though an error for using any appears instead).
const obj: {
foo: any;
} = {
foo: async () => {
return 3.14;
},
}
export async function main() {
obj.foo();
}
From this, we can see that it correctly infers the "type of obj" and, through that, infers the type of the return value of obj.foo.
Trying difficult types
Now, let's be a bit mischievous and try some of TypeScript's more complex types.
Generics
First is an example using generics.
function id<T>(x: T): T {
return x;
}
async function foo(): Promise<number> {
return 3.14;
}
export async function main() {
id(foo());
}
In this example, the return value of id(foo()) is a Promise, but this cannot be understood without performing type inference on generics.
This was the most surprising result for me: Biome actually detected a lint error for this example. In other words, it seems to recognize that the return value of id(foo()) is a Promise. The lint error is correctly detected for id rather than foo() itself.
The inference rules for generics in TypeScript are extremely complex, so I don't expect Biome to reproduce all of them, but it seems that type inference using generics is possible in simple cases like this one.
Lookup Types
Lookup types refer to types with syntax like T[K].
interface Obj {
noPromise: number;
yesPromise: Promise<number>;
}
const obj: Obj = {
noPromise: 3.14,
yesPromise: Promise.resolve(3.14),
}
function foo<K extends keyof Obj>(key: K): Obj[K] {
return obj[key];
}
export async function main() {
foo('noPromise');
foo('yesPromise');
}
In this case, the return value of foo('noPromise') is of type number, while the return value of foo('yesPromise') is of type Promise<number>. Can Biome see through this?
The answer is, unfortunately, no lint error was detected for foo('yesPromise'). It seems that computing lookup types is not supported.
By the way, it also failed in simple cases like the following, even without involving generics:
interface Obj {
noPromise: number;
yesPromise: Promise<number>;
}
async function foo(): Obj["yesPromise"] {
return 3.14;
}
export async function main() {
foo();
}
Mapped Types and Conditional Types
Since Lookup types didn't work, I assumed the rest would be impossible as well, but I tried them just in case. As expected, they were not supported.
// Mapped type
type Raw = {
noPromise: number;
yesPromise: Promise<number>;
}
type Obj = {
[K in keyof Raw]: () => Raw[K];
}
const obj: Obj = {
noPromise: () => 42,
yesPromise: () => Promise.resolve(3.14),
}
export async function main() {
obj.yesPromise();
}
// Conditional type
function foo<K extends string | number>(key: K): K extends string ? number : Promise<number> {
if (typeof key === 'string') {
return 42 as any;
} else {
return Promise.resolve(42) as any;
}
}
export async function main() {
foo(123);
}
Union and Intersection Types
Union types are crucial for the practical application of TypeScript. Let's try a case that includes them.
function foo(): number | Promise<number> {
if (Math.random() > 0.5) {
return 42;
}
return new Promise((resolve) => {
setTimeout(() => resolve(42), 1000);
});
}
export async function main() {
foo();
}
In this case, foo() might be a Promise or it might not.
Biome detected a lint error for this example. In other words, it seems to recognize the possibility of a Promise being part of a union type.
Now, let's also try intersection types.
function foo(): Promise<number> & { abort: () => void} {
return {} as any; // Omitted
}
export async function main() {
foo();
}
For this one, no lint error was detected. It seems it doesn't recognize that there is a Promise as part of the intersection type. However, whether this should be a target for detection is debatable. Moreover, since there is no inherent difficulty in detection, it seems like something that could be supported relatively easily.
Trying Type Aliases
Will it be detected if we give the Promise type an alias?
type PromiseNumber = Promise<number>;
function foo(): PromiseNumber {
return new Promise((resolve) => {
setTimeout(() => resolve(42), 1000);
});
}
export async function main() {
foo();
}
In this example, it was detected. Even if complex type computations are impossible, it seems that type aliases can be recognized.
Conclusion
In this article, I investigated the performance of Biome v2's "type inference" as it stands today.
As a result, I found that its performance is quite modest compared to the type inference provided by a full type checker, as evidenced by the fact that it required type annotations in the return new Promise example where it failed to infer the type on its own.
Even so, I observed behaviors that can truly be called inference—such as the internal handling of object and function types, the ability to process generics, and the recognition of async function return types as Promise even without explicit annotations. It doesn't feel like a different mechanism masquerading as type inference; rather, it feels like it is being built with a proper focus on the concept of "types."
However, even when type annotations are explicitly provided, there are limits to the type calculations. It appeared impossible to compute things like lookup types or conditional types.
It is surprising that, as mentioned in the official blog, such modest performance is sufficient to detect 75% of cases.
I had a concern regarding Biome and other TypeScript-independent "type inference" features: that coding rules might spread which encourage writing only what Biome can understand, rather than fully utilizing the TypeScript type system. If that happens, the TypeScript type system would effectively fork.
While Biome is still in the middle of its evolution, what did you think of these verification results? Did they make you want to change how you operate TypeScript to align with Biome?
I personally intend to continue keeping an eye on how things develop from here.
Discussion
biome type inference記事をありがとうございます! 大変参考になりました。もし可能であれば、この記事を他のプラットフォームに翻訳・転載させていただけないでしょうか?世界中の方々とこの知見を共有したいと考えています。
ありがとうございます! 翻訳・転載して構いません。
どうもありがとう!翻訳した記事はこちらで, 質問または指摘は大歓迎です