Types

TranslationResult

Type definition for translation results returned by translate() methods

Overview

TranslationResult represents the result of translation operations.

type TranslationResult = RequestSuccess | TranslationError;

Union Types

RequestSuccess

type RequestSuccess = TypedResult & {
  locale: string;
  reference: TranslationResultReference;
};

TranslationError

type TranslationError = {
  error: string;
  code: number;
  reference?: TranslationResultReference;
};

Examples

Basic Error Handling

import { GT, TranslationResult } from 'generaltranslation';

const gt = new GT({ apiKey: 'your-api-key' });

const result: TranslationResult = await gt.translate('Hello');

if ('error' in result) {
  console.error(`Translation failed: ${result.error}`);
} else {
  console.log(`Translation: ${result.translation}`);
}

Type Guards

function isTranslationError(result: TranslationResult): result is TranslationError {
  return 'error' in result;
}

if (isTranslationError(result)) {
  // Handle error
} else {
  // Handle success
}

How is this guide?

TranslationResult