Special Sponsor:PromptBuilder— Fast, consistent prompt creation powered by 1,000+ expert templates.
Make your Product visible here.Contact Us

Home/TypeScript Errors/TS2538

TS2538

Type 'X' cannot be used as an index type

TypeScript TS2538 fires when you try to use a value as an object index (bracket notation) but its type is not a valid index type. Only string, number, and symbol are valid index types in TypeScript.

Why This Happens

You're using a value as obj[key] where the key's type is boolean, null, undefined, an object, or another type that isn't a valid property key. TypeScript requires index types to be string, number, or symbol.

Code That Triggers TS2538

// TS2538 examples

interface Config { port: number; host: string }
const key: boolean = true;
const cfg: Config = { port: 3000, host: "localhost" };
cfg[key]; // Error: Type 'boolean' cannot be used as an index type

// Also with null/undefined
function getValue(obj: Record<string, number>, key: string | null) {
  return obj[key]; // Error: Type 'string | null' cannot be used as an index type
}

// Object as key
const objKey = { id: 1 };
const map = {};
map[objKey]; // Error: Type '{ id: number }' cannot be used as an index type

How to Fix TS2538

Option 1: Narrow the type to a valid index type

function getValue(obj: Record<string, number>, key: string | null) {
  if (key === null) return undefined;
  return obj[key]; // OK — narrowed to string
}

Option 2: Cast to string/number before indexing

const index = someValue as string;
obj[index]; // OK

Option 3: Use Map for object-keyed lookups

const map = new Map<{ id: number }, string>();
map.set({ id: 1 }, "value"); // objects can be Map keys
map.get(key);

Frequently Asked Questions — TS2538

What does TS2538 mean?

TS2538 means you're using a value as an object index (obj[key]) where the key's type isn't a valid index type. Only string, number, and symbol are valid. Fix by narrowing or casting the key.

Why can't boolean be used as an index type?

JavaScript object property keys are always coerced to strings. Booleans become the strings 'true' or 'false'. TypeScript disallows boolean as an index type to make this explicit and prevent confusion.

TS2538 with string | null — how to fix?

Check for null before indexing: if (key !== null) { obj[key] }. TypeScript's control flow analysis narrows the type to string after the null check.

Can I use objects as Map keys in TypeScript?

Yes — use Map<KeyType, ValueType> instead of plain objects. Maps accept any value as a key including objects, and TypeScript types them correctly.

TS2538 with keyof — when does it help?

Using keyof ensures the key type is a valid property of the object: function get<T>(obj: T, key: keyof T). This prevents both TS2538 and TS2339 at once.

Convert JavaScript to TypeScript automatically

Paste your JS code and get type-annotated TypeScript — including fixes for common type errors — in seconds.

Try the converter →

Other TypeScript Errors

From the blog

View all →