Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix type narrowing for listener #84

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading