Code completion and IntelliSense API reference with real examples from completions.ts
The Completions API provides IntelliSense features like auto-complete suggestions, parameter hints, and import completions. This powers the dropdown suggestion list in editors.
interface GetCompletionsAtPositionOptions { // Trigger character that caused completions (e.g., '.', '@') triggerCharacter?: CompletionsTriggerCharacter; // Why completions were requested triggerKind?: CompletionTriggerKind; // Include module exports as auto-imports includeCompletionsForModuleExports?: boolean; // Include completions with additional text insertion includeCompletionsWithInsertText?: boolean; // Include snippets like class member templates includeCompletionsWithSnippetText?: boolean; // Include automatic optional chain completions includeAutomaticOptionalChainCompletions?: boolean; // Include completions for import statements includeCompletionsForImportStatements?: boolean; // Include package.json dependencies includePackageJsonAutoImports?: 'auto' | 'on' | 'off';}
interface CompletionInfo { // Whether the list is complete or partial isGlobalCompletion: boolean; // Whether completions are for an object member (e.g., after '.') isMemberCompletion: boolean; // Whether typing an identifier at this location isNewIdentifierLocation: boolean; // Whether more completions might be available isIncomplete?: boolean; // List of completion entries entries: CompletionEntry[]; // Optional span to replace when completing optionalReplacementSpan?: TextSpan; // Default characters that commit a completion defaultCommitCharacters?: string[];}
interface CompletionEntry { // Name of the completion (displayed in list) name: string; // Kind of symbol (function, variable, etc.) kind: ScriptElementKind; // Modifiers (optional, readonly, etc.) kindModifiers?: string; // Sort order for displaying sortText: string; // Text to insert (if different from name) insertText?: string; // Is this a snippet with placeholders? isSnippet?: boolean; // Replacement span for this completion replacementSpan?: TextSpan; // Does this completion have a code action? hasAction?: boolean; // Source of the completion (for auto-imports) source?: string; // Display info for source module sourceDisplay?: SymbolDisplayPart[]; // Label details (signature, type, etc.) labelDetails?: CompletionEntryLabelDetails; // Whether the symbol is deprecated isDeprecated?: boolean; // Is this a recommended completion? isRecommended?: boolean; // Data for resolving details later data?: CompletionEntryData;}
interface CompletionEntryDetails { // Name of the completion name: string; // Kind of symbol kind: ScriptElementKind; // Modifiers kindModifiers: string; // Full signature/type displayParts: SymbolDisplayPart[]; // JSDoc documentation documentation?: SymbolDisplayPart[]; // JSDoc tags (@param, @returns, etc.) tags?: JSDocTagInfo[]; // Code actions to perform when accepting this completion codeActions?: CodeAction[]; // Source of the completion source?: SymbolDisplayPart[]; // Source location of the symbol sourceDisplay?: SymbolDisplayPart[];}
Signature: function add(a: number, b: number): numberDocumentation: Adds two numbers together.@param a: The first number@param b: The second number@returns: The sum of a and bCode actions: app.ts: import { add } from "./math";
When options.allowIncompleteCompletions is true, the Language Service may return partial results:
const completions = service.getCompletionsAtPosition( 'app.ts', position, { includeCompletionsForModuleExports: true, // Allow incomplete for better responsiveness });if (completions?.isIncomplete) { // Some completions were skipped for performance // Request again to get full list const fullCompletions = service.getCompletionsAtPosition( 'app.ts', position, options, formattingSettings );}