Field Selection
Field selection lets you request only the properties you need from the API. This reduces response size and provides automatic TypeScript type narrowing — the return type only includes the fields you selected.
Basic Field Selection
Section titled “Basic Field Selection”Both retrieve() and list() accept a fieldList option:
const issue = await client.issue.retrieve(12345, { fieldList: ['id', 'name', 'issueNumber', 'coverDate', 'volume'],});
// TypeScript knows exactly what fields are availableconsole.log(issue.id); // numberconsole.log(issue.name); // stringconsole.log(issue.issueNumber); // numberconsole.log(issue.description); // TypeScript error - not selectedField Selection with Lists
Section titled “Field Selection with Lists”Field selection works the same way for list operations:
const characters = await client.character.list({ fieldList: ['id', 'name', 'image', 'publisher'], filter: { name: 'Spider-Man' },});
characters.results.forEach((character) => { console.log(character.name); // Available console.log(character.image); // Available console.log(character.description); // TypeScript error});Why Use Field Selection?
Section titled “Why Use Field Selection?”- Smaller responses — The API only returns the fields you request, reducing bandwidth
- Type safety — TypeScript prevents you from accessing fields that weren’t requested
- Better IntelliSense — Auto-completion only shows available fields
- Compile-time validation — Catch mistakes before runtime