@typescript-eslint/no-unsafe-member-access

Disallow reading a property of a value of type any

needs types

Also called typescript-eslint/no-unsafe-member-access, typescript/no-unsafe-member-access

TypeScript doesn't check a property that you read from an any value. A typo in the name, or a missing object in the middle of a chain, only shows up when the code runs. The result is any again, so the code that uses it isn't checked either.

In a chain such as response.user.name, only the first unsafe step is reported.

bun lint --type-aware turns this rule on in TypeScript files.

Configure#

Off by default. This rule needs types, so turning it on makes bun lint type check your code first.

bunfig.toml
[lint.rules]
"@typescript-eslint/no-unsafe-member-access" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-unsafe-member-access" = ["error", {
  allowOptionalChaining = false,
}]

Examples#

example.ts1 problem
function readName(response: any) {
  return response.user.name;
}
example.tsNo problems
interface UserResponse {
  user: { name: string };
}

function readName(response: UserResponse) {
  return response.user.name;
}

An any value used as the key is reported too, because the result can be any property of the object:

example.ts1 problem
function pick(prices: number[], key: any) {
  return prices[key];
}

Options#

OptionDefaultWhat it does
allowOptionalChainingfalseAllow ?. on an any value.
example.tswith { "allowOptionalChaining": true }No problems
function readUser(response: any) {
  return response?.user;
}

Each step needs its own ?.. Here .name is still reported:

example.tswith { "allowOptionalChaining": true }1 problem
function readName(response: any) {
  return response?.user.name;
}