Types
TranslationResult
Type definition for translation results returned by translate() methods
Overview
TranslationResult represents the outcome 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
}Related types
TranslateManyResult- Batch translation results
How is this guide?