Skip to content

Commit

Permalink
fix type narrowing for listener
Browse files Browse the repository at this point in the history
  • Loading branch information
khromov committed Mar 26, 2024
1 parent f11750d commit 8984550
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/definitions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import type { PermissionState, PluginListenerHandle } from '@capacitor/core';

type EventName = 'partialResults' | 'listeningState';

type EventData<T> = T extends 'partialResults'
? { matches: string[] }
: T extends 'listeningState'
? { status: 'started' | 'stopped' }
: never;

export interface PermissionStatus {
/**
* Permission state for speechRecognition alias.
Expand Down Expand Up @@ -81,28 +89,21 @@ export interface SpeechRecognitionPlugin {
*/
requestPermissions(): Promise<PermissionStatus>;
/**
* Called when partialResults set to true and result received.
* Event: partialResults Called when partialResults set to true and result received.
* Event: listeningState Called when listening state changed.
*
* On Android it doesn't work if popup is true.
*
* Provides partial result.
*
* @since 2.0.2
* @since 2.0.2 (partialResults)
* @since 6.0.0 (listeningState)
*/
addListener(
eventName: 'partialResults',
listenerFunc: (data: { matches: string[] }) => void,
addListener<T extends EventName>(
eventName: T,
listenerFunc: (data: EventData<T>) => void,
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Called when listening state changed.
*
* @since 6.0.0
*/
addListener(
eventName: 'listeningState',
listenerFunc: (data: { status: 'started' | 'stopped' }) => void,
): Promise<PluginListenerHandle> & PluginListenerHandle;
/**
* Remove all the listeners that are attached to this plugin.
*
Expand Down

0 comments on commit 8984550

Please sign in to comment.