Skip to content

Commit

Permalink
Fix type declaration for typescript 4.2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ak1394 committed Mar 17, 2021
1 parent 4f7c6bb commit 110d818
Showing 1 changed file with 94 additions and 59 deletions.
153 changes: 94 additions & 59 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,117 @@
import * as RN from 'react-native'
import * as RN from "react-native";

type SimpleEvents = 'tts-start' | 'tts-finish' | 'tts-error' | 'tts-cancel'
type SimpleEvents = "tts-start" | "tts-finish" | "tts-error" | "tts-cancel";
type SimpleEvent = {
utteranceId: string | number
}
utteranceId: string | number;
};

type ProgressEventName = 'tts-progress'
type ProgressEventName = "tts-progress";
type ProgressEvent = {
utteranceId: string | number
location: number
length: number
}
utteranceId: string | number;
location: number;
length: number;
};

export type TtsEvents = SimpleEvents | ProgressEventName
export type TtsEvent<T extends TtsEvents = TtsEvents> = T extends ProgressEventName ? ProgressEvent : SimpleEvent
export type TtsEventHandler<T extends TtsEvents= TtsEvents> = (event: TtsEvent<T>) => any
export type TtsEvents = SimpleEvents | ProgressEventName;
export type TtsEvent<
T extends TtsEvents = TtsEvents
> = T extends ProgressEventName ? ProgressEvent : SimpleEvent;
export type TtsEventHandler<T extends TtsEvents = TtsEvents> = (
event: TtsEvent<T>
) => any;

export type TtsError = {
code: 'no_engine' | 'error' | 'not_ready' | 'invalid_request' | 'network_error' | 'network_timeout' | 'not_installed_yet' | 'output_error'
| 'service_error' | 'synthesis_error' | 'lang_missing_data' | 'lang_not_supported' | 'Android AudioManager error' | 'not_available' | 'not_found'
| 'bad_rate'
message: string
}
code:
| "no_engine"
| "error"
| "not_ready"
| "invalid_request"
| "network_error"
| "network_timeout"
| "not_installed_yet"
| "output_error"
| "service_error"
| "synthesis_error"
| "lang_missing_data"
| "lang_not_supported"
| "Android AudioManager error"
| "not_available"
| "not_found"
| "bad_rate";
message: string;
};

export type Voice = {
id: string
name: string
language: string
quality: number
latency: number
networkConnectionRequired: boolean
notInstalled: boolean
}
id: string;
name: string;
language: string;
quality: number;
latency: number;
networkConnectionRequired: boolean;
notInstalled: boolean;
};

export type Engine = {
name: string
label: string
default: boolean
icon: number
}
name: string;
label: string;
default: boolean;
icon: number;
};

export type AndroidOptions = {
/** Parameter key to specify the audio stream type to be used when speaking text or playing back a file */
KEY_PARAM_STREAM: 'STREAM_VOICE_CALL' | 'STREAM_SYSTEM' | 'STREAM_RING' | 'STREAM_MUSIC' | 'STREAM_MUSIC' | 'STREAM_ALARM' | 'STREAM_NOTIFICATION' | 'STREAM_DTMF' | 'STREAM_ACCESSIBILITY'
KEY_PARAM_STREAM:
| "STREAM_VOICE_CALL"
| "STREAM_SYSTEM"
| "STREAM_RING"
| "STREAM_MUSIC"
| "STREAM_MUSIC"
| "STREAM_ALARM"
| "STREAM_NOTIFICATION"
| "STREAM_DTMF"
| "STREAM_ACCESSIBILITY";
/** Parameter key to specify the speech volume relative to the current stream type volume used when speaking text. Volume is specified as a float ranging from 0 to 1 where 0 is silence, and 1 is the maximum volume (the default behavior). */
KEY_PARAM_VOLUME: number
KEY_PARAM_VOLUME: number;
/** Parameter key to specify how the speech is panned from left to right when speaking text. Pan is specified as a float ranging from -1 to +1 where -1 maps to a hard-left pan, 0 to center (the default behavior), and +1 to hard-right. */
KEY_PARAM_PAN: number
}
KEY_PARAM_PAN: number;
};

export type Options = string | {
iosVoiceId: string
rate: number
androidParams: AndroidOptions
}
export type Options =
| string
| {
iosVoiceId: string;
rate: number;
androidParams: AndroidOptions;
};

export class ReactNativeTts extends RN.NativeEventEmitter {
getInitStatus: () => Promise<'success'>
requestInstallEngine: () => Promise<'success'>
requestInstallData: () => Promise<'success'>
setDucking: (enabled: boolean) => Promise<'success'>
setDefaultEngine: (engineName: string) => Promise<boolean>
setDefaultVoice: (voiceId: string) => Promise<'success'>
setDefaultRate: (rate: number, skipTransform?: boolean) => Promise<'success'>
setDefaultPitch: (pitch: number) => Promise<'success'>
setDefaultLanguage: (language: string) => Promise<'success'>
setIgnoreSilentSwitch: (ignoreSilentSwitch: boolean) => Promise<boolean>
voices: () => Promise<Voice[]>
engines: () => Promise<Engine[]>
getInitStatus: () => Promise<"success">;
requestInstallEngine: () => Promise<"success">;
requestInstallData: () => Promise<"success">;
setDucking: (enabled: boolean) => Promise<"success">;
setDefaultEngine: (engineName: string) => Promise<boolean>;
setDefaultVoice: (voiceId: string) => Promise<"success">;
setDefaultRate: (rate: number, skipTransform?: boolean) => Promise<"success">;
setDefaultPitch: (pitch: number) => Promise<"success">;
setDefaultLanguage: (language: string) => Promise<"success">;
setIgnoreSilentSwitch: (ignoreSilentSwitch: boolean) => Promise<boolean>;
voices: () => Promise<Voice[]>;
engines: () => Promise<Engine[]>;
/** Read the sentence and return an id for the task. */
speak: (utterance: string, options?: Options) => string | number
stop: (onWordBoundary?: boolean) => Promise<boolean>
pause: (onWordBoundary?: boolean) => Promise<boolean>
resume: () => Promise<boolean>
addEventListener: <T extends TtsEvents>(type: T, handler: TtsEventHandler<T>) => void
removeEventListener: <T extends TtsEvents>(type: T, handler: TtsEventHandler<T>) => void
speak: (utterance: string, options?: Options) => string | number;
stop: (onWordBoundary?: boolean) => Promise<boolean>;
pause: (onWordBoundary?: boolean) => Promise<boolean>;
resume: () => Promise<boolean>;
addEventListener: <T extends TtsEvents>(
type: T,
handler: TtsEventHandler<T>
) => void;
removeEventListener: <T extends TtsEvents>(
type: T,
handler: TtsEventHandler<T>
) => void;
}

declare const Tts = new ReactNativeTts();
declare const Tts: ReactNativeTts;

export default Tts
export default Tts;

0 comments on commit 110d818

Please sign in to comment.