Type entity identifiers
Typing IDs returned by entity selectors or model-specific ID selectors.
Redux Toolkit · Type Alias
EntityId is Redux Toolkit's type for an entity identifier. It is defined as number | string, covering numeric and textual IDs used by normalized entity state.
EntityId is a union type for identifiers used by Redux Toolkit's entity-state APIs. An identifier may be either a number or a string, so it can represent numeric database keys and textual keys alike.
export type EntityId = number | stringimport type { EntityId } from '@reduxjs/toolkit'
const bookId: EntityId = 'book-1'
const otherId: EntityId = 42
console.log(bookId, otherId)This TypeScript example assigns both supported EntityId forms: a string and a number.
Typing IDs returned by entity selectors or model-specific ID selectors.
Representing normalized adapter state with IDs and keyed records.
Supporting APIs whose identifiers are either numeric or textual.
EntityId is the shared identifier type used by Redux Toolkit's entity APIs. Its definition is the union number | string, allowing either numeric or textual IDs.
Follow-up: Why is a union preferable here? It preserves type checking while supporting common identifier formats without forcing every application to convert IDs.
An entity adapter stores IDs in its normalized ids collection, while entity records are keyed by those identifiers in entities. EntityId describes the allowed ID values for that normalized state.
Follow-up: What state shape uses these IDs? ids is an EntityId array, and entities maps EntityId keys to entity values.
IdSelector<T> returns an EntityId for a model, and an adapter's selectId callback identifies each entity. The default selector reads entity.id, but a custom selector is needed when the ID is stored elsewhere.
Follow-up: When should a custom selector be supplied? Use one whenever the entity's unique identifier is not in its id field.
createEntityAdapterEntityStateIdSelectorComparerselectId