Skip to content

Conversation

@arendjr
Copy link
Contributor

@arendjr arendjr commented May 26, 2025

Summary

This PR implements 2 major improvements that are especially important in interpreting .d.ts files (but may benefit other code too), and has some minor improvements too.

Nested type qualifiers can be resolved

For instance, the following will now resolve correctly:

namespace Foo {
    type Bar = string;
}

type Alias = Foo.Bar; // This works now.

Type/value duality is correctly handled

A new TypeData::DualReference variant has been introduced that can simultaneously reference a type, as well as the type of a value.

const A = "a";

type A = 1;

type Alias = typeof A; // Resolves to "a".
type Alias2 = A;       // Resolves to 1.

Dual types can even be exported and imported too.

I also discovered the tests for useExhaustiveSwitchCases contained some cases that used to have incorrect results because of this duality, which are now fixed too. cc @siketyan

Improved operator inference

Inference has improved for a few operators, especially the typeof operator, but also other unary operators such as unary minus (-) and bitwise not (~).

Reordered global type IDs in snapshots

Global type IDs are always a bit confusing in snapshots, because we have predefined global types that we don't wish to be part of the snapshots. This meant global type IDs would always start counting from a seemingly arbitrary index, such as 17. Now they always start at 0, which should also help to make snapshots change less going forward. The downside is that for global type IDs, the numbers in the snapshots no longer match the internal type IDs.

Test Plan

Test cases added and snapshots updated.

@arendjr arendjr requested review from a team May 26, 2025 17:46
@github-actions github-actions bot added A-Project Area: project A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels May 26, 2025
@arendjr arendjr moved this to In review in Type Inference May 26, 2025
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice this incorrect result 🤯 Thank you for the fix!

@arendjr arendjr merged commit b809906 into biomejs:main May 27, 2025
11 checks passed
@github-project-automation github-project-automation bot moved this from In review to Done in Type Inference May 27, 2025
ematipico pushed a commit that referenced this pull request May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter A-Project Area: project L-JavaScript Language: JavaScript and super languages

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants