Skip to content

Clean up as any type assertions in the codebase #269213

Open

Description

@mjbvz

We've seen a few build breaks caused by as any type assertions in our codebase. This can hide errors and break after mangling. Some language models seem to be specially keen on adding as any assertions to work around problems. This can break our mangling and results in more fragile code

We'd like to remove these as any cast in as many

Plan

The first priority is preventing new code from using as any. We'll then start addressing existing usages. Once complete we can also consider adding even stricter rules for type assertions

  • Matt Bierner (@mjbvz) add a new eslint rule for a as any and <any>.
  • Matt Bierner (@mjbvz) Will add suppression comments for all existing breakers. There are around 900 or so
  • 🏃 Matt Bierner (@mjbvz) Will update this issue with a list of all files that have breaks
  • 🏃 Assigned team member review and fix these errors during debt week

Once complete:

  • Evaluate if we also want to enabled the @typescript-eslint/no-unsafe-type-assertion option for even stricter rules

Fixing as any

To find places in your code that need fixing, just search for: // eslint-disable-next-line local/code-no-any-casts

Here are some general patterns you can use to replace as any in code:

Use real type guards

This test code was using as any to simplify writing the test:

const tokens = store.root as any;
assert.strictEqual(tokens.children[0].token, 1);

Ww can use a real type check with instanceof to get correct error reporting and Intellisense:

const tokens = store.root;
assert.ok(tokens instanceof ListNode);

assert.strictEqual(tokens.children[0].token, 1);

Use more correct types

This as any was added because we need to cast an string to a AXPropertyName (a string union):

function createAXProperty(name: string, value: any, type: AXValueType = 'string'): AXProperty {
	return {
		name: name as any,
		value: createAXValue(type, value)
	};
}

If we just use proper types, we don't need the assertion:

function createAXProperty(name: AXPropertyName, value: any, type: AXValueType = 'string'): AXProperty {
	return {
		name: name,
		value: createAXValue(type, value)
	};
}

Add better types

This code uses as any to access some internal types:

// HACK: xterm.js doesn't expose this by design as it's an internal core
// function an embedder could easily do damage with. Additionally, this
// can't really be upstreamed since the event relies on shell integration to
// verify the shifting is necessary.
(this._terminal as any)._core._bufferService.buffer.lines.onDeleteEmitter.fire({
	index: this._terminal.buffer.active.baseY,
	amount: potentialShiftedLineCount
});

A better approach is to declare the shape of the type we're accessing:

interface ITerminalInternals extends Terminal {
	_core: ...;
}
	
// HACK: xterm.js doesn't expose this by design as it's an internal core
// function an embedder could easily do damage with. Additionally, this
// can't really be upstreamed since the event relies on shell integration to
// verify the shifting is necessary.
(this._terminal as ITerminalInternals)._core._bufferService.buffer.lines.onDeleteEmitter.fire({
	index: this._terminal.buffer.active.baseY,
	amount: potentialShiftedLineCount
});

You can also consider writing a helper function that returns obj is ITerminalInternals:

function isTerminalInterals(obj: unknown): obj is ITerminalInternals {
  return !!(obj as ITerminalInternals)._core;
}
const term = this.terminal;
if (isTerminalInternals(term)) {
  // term will for sure be an ITerminalInternals
}

Use unknown

If you are dealing with a value whose type you don't care about, use unknown instead. This will prevent accessing properties on the value

Assignments

Please reassign if these are incorrect

Built-in extension

Build scripts (/build/**/*)

Workbench/contrib

Activity

  1. added
    debt
    on Sep 30, 2025
  2. changed the title [-]Clean up `as any` type assertions in the Codebase[/-] [+]Clean up `as any` type assertions in the codebase[/+] on Sep 30, 2025
  3. bpasero commented on Oct 1, 2025

    @bpasero
    Contributor

    💯 to the use of unknown, I am often able to just throw that into places where the value just flows through the system without being used.

  4. meganrogge commented on Oct 1, 2025

    @meganrogge
    Collaborator

    TIL I learned about the difference between unknown and any!

  5. bpasero commented on Oct 1, 2025

    @bpasero
    Contributor

    Megan Rogge (@meganrogge) glad you learned it now, we need to collect more of the best practices and share broader with the team!

  6. 61 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

debt

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions