Skip to content

Commit ccf35da

Browse files
authored
Add more typings (#224)
* Add typings * Fix typing * Clean up typings * Add barrel file * Add entry * Moved entry
1 parent 58326a8 commit ccf35da

22 files changed

+937
-676
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1515

1616
### Changed
1717

18-
- 💥 Modernized some code with TypeScript, more type-aligned to W3C Speech API, and moved to official Event Target API, in PR [#220](https://github.com/compulim/web-speech-cognitive-services/pull/220)
18+
- 💥 Modernized some code with TypeScript, more type-aligned to W3C Speech API, and moved to official Event Target API, in PR [#220](https://github.com/compulim/web-speech-cognitive-services/pull/220) and [#224](https://github.com/compulim/web-speech-cognitive-services/pull/224)
1919
- `SpeechRecognitionResult` and `SpeechRecognitionResultList` is now a array-like object, use `Array.from()` to convert them into an array
2020
- Updated build tools and added named exports via CJS/ESM
2121
- Bumped dependencies, in PR [#216](https://github.com/compulim/web-speech-cognitive-services/pull/216) and [#218](https://github.com/compulim/web-speech-cognitive-services/issues/218)

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import createSpeechRecognitionPonyfill, {
2-
createSpeechRecognitionPonyfillFromRecognizer
3-
} from './SpeechToText/createSpeechRecognitionPonyfill';
1+
import { createSpeechRecognitionPonyfill, createSpeechRecognitionPonyfillFromRecognizer } from './SpeechToText/index';
42

53
export default createSpeechRecognitionPonyfill;
64

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechGrammarList.js renamed to packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechGrammarList.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
/* eslint class-methods-use-this: "off" */
22

3-
export default class {
3+
export default class SpeechGrammarList {
44
constructor() {
5-
this._phrases = [];
5+
this.#phrases = [];
66
}
77

88
addFromString() {
99
throw new Error('JSGF is not supported');
1010
}
1111

12-
get phrases() {
13-
return this._phrases;
12+
#phrases: readonly string[];
13+
14+
get phrases(): readonly string[] {
15+
return this.#phrases;
1416
}
1517

16-
set phrases(value) {
18+
set phrases(value: readonly string[]) {
1719
if (Array.isArray(value)) {
18-
this._phrases = value;
20+
this.#phrases = Object.freeze([...value]);
1921
} else if (typeof value === 'string') {
20-
this._phrases = [value];
22+
this.#phrases = Object.freeze([value]);
2123
} else {
2224
throw new Error(`The provided value is not an array or of type 'string'`);
2325
}

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionErrorEvent.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export type SpeechRecognitionErrorType =
66
| 'network'
77
| 'no-speech'
88
| 'not-allowed'
9-
| 'service-not-allowed';
9+
| 'service-not-allowed'
10+
| 'unknown';
1011

1112
export type SpeechRecognitionErrorEventInit = {
1213
error: SpeechRecognitionErrorType;
@@ -31,4 +32,8 @@ export default class SpeechRecognitionErrorEvent extends Event {
3132
get message(): string | undefined {
3233
return this.#message;
3334
}
35+
36+
override get type(): 'error' {
37+
return 'error';
38+
}
3439
}

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionEvent.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ export default class SpeechRecognitionEvent<
5454
get results(): SpeechRecognitionResultList {
5555
return this.#results;
5656
}
57+
58+
override get type(): T {
59+
return super.type as T;
60+
}
5761
}

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionEventListenerMap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import EventListenerMap from './EventListenerMap';
1+
import EventListenerMap from './private/EventListenerMap';
22
import type SpeechRecognitionErrorEvent from './SpeechRecognitionErrorEvent';
33
import type SpeechRecognitionEvent from './SpeechRecognitionEvent';
44

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionResult.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import FakeArray from './FakeArray';
1+
import FakeArray from './private/FakeArray';
22

33
export type SpeechRecognitionResultInit = {
44
isFinal: boolean;

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/SpeechRecognitionResultList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import FakeArray from './FakeArray';
1+
import FakeArray from './private/FakeArray';
22
import type SpeechRecognitionResult from './SpeechRecognitionResult';
33

44
export default class SpeechRecognitionResultList extends FakeArray<SpeechRecognitionResult> {

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/cognitiveServiceEventResultToWebSpeechRecognitionResult.test.js

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import cognitiveServiceEventResultToWebSpeechRecognitionResult from './cognitive
66

77
test('Multiple results with RecognitionStatus === "Success"', () => {
88
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult({
9+
duration: 0,
10+
errorDetails: '',
911
json: {
1012
NBest: [
1113
{
@@ -25,7 +27,11 @@ test('Multiple results with RecognitionStatus === "Success"', () => {
2527
],
2628
RecognitionStatus: 'Success'
2729
},
28-
reason: 3
30+
offset: 0,
31+
properties: '',
32+
reason: 3,
33+
resultId: '',
34+
text: ''
2935
});
3036

3137
expect(resultList[0]).toEqual(expect.objectContaining({ confidence: 0.25, transcript: 'No.' }));
@@ -35,7 +41,13 @@ test('Multiple results with RecognitionStatus === "Success"', () => {
3541

3642
test('Single interim results', () => {
3743
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult({
44+
duration: 0,
45+
errorDetails: '',
46+
json: {},
47+
offset: 0,
48+
properties: '',
3849
reason: 2,
50+
resultId: '',
3951
text: 'No.'
4052
});
4153

@@ -45,6 +57,8 @@ test('Single interim results', () => {
4557

4658
test('Single final results', () => {
4759
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult({
60+
duration: 0,
61+
errorDetails: '',
4862
json: {
4963
NBest: [
5064
{
@@ -56,7 +70,11 @@ test('Single final results', () => {
5670
}
5771
]
5872
},
59-
reason: 3
73+
offset: 0,
74+
properties: '',
75+
reason: 3,
76+
resultId: '',
77+
text: ''
6078
});
6179

6280
expect(resultList[0]).toEqual(expect.objectContaining({ confidence: 0.25, transcript: 'No.' }));
@@ -66,6 +84,8 @@ test('Single final results', () => {
6684
test('Single final results with ITN', () => {
6785
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult(
6886
{
87+
duration: 0,
88+
errorDetails: '',
6989
json: {
7090
NBest: [
7191
{
@@ -77,9 +97,14 @@ test('Single final results with ITN', () => {
7797
}
7898
]
7999
},
80-
reason: 3
100+
offset: 0,
101+
properties: '',
102+
reason: 3,
103+
resultId: '',
104+
text: ''
81105
},
82106
{
107+
maxAlternatives: 1,
83108
textNormalization: 'itn'
84109
}
85110
);
@@ -91,6 +116,8 @@ test('Single final results with ITN', () => {
91116
test('Single final results with lexical', () => {
92117
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult(
93118
{
119+
duration: 0,
120+
errorDetails: '',
94121
json: {
95122
NBest: [
96123
{
@@ -102,9 +129,14 @@ test('Single final results with lexical', () => {
102129
}
103130
]
104131
},
105-
reason: 3
132+
offset: 0,
133+
properties: '',
134+
reason: 3,
135+
resultId: '',
136+
text: ''
106137
},
107138
{
139+
maxAlternatives: 1,
108140
textNormalization: 'lexical'
109141
}
110142
);
@@ -116,6 +148,8 @@ test('Single final results with lexical', () => {
116148
test('Single final results with masked ITN', () => {
117149
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult(
118150
{
151+
duration: 0,
152+
errorDetails: '',
119153
json: {
120154
NBest: [
121155
{
@@ -127,9 +161,14 @@ test('Single final results with masked ITN', () => {
127161
}
128162
]
129163
},
130-
reason: 3
164+
offset: 0,
165+
properties: '',
166+
reason: 3,
167+
resultId: '',
168+
text: ''
131169
},
132170
{
171+
maxAlternatives: 1,
133172
textNormalization: 'maskeditn'
134173
}
135174
);
@@ -141,6 +180,8 @@ test('Single final results with masked ITN', () => {
141180
test('Result is iterable', () => {
142181
const resultList = cognitiveServiceEventResultToWebSpeechRecognitionResult(
143182
{
183+
duration: 0,
184+
errorDetails: '',
144185
json: {
145186
NBest: [
146187
{
@@ -152,9 +193,16 @@ test('Result is iterable', () => {
152193
}
153194
]
154195
},
155-
reason: 3
196+
offset: 0,
197+
properties: '',
198+
reason: 3,
199+
resultId: '',
200+
text: ''
156201
},
157-
{}
202+
{
203+
maxAlternatives: 1,
204+
textNormalization: 'display'
205+
}
158206
);
159207

160208
const [firstAlternative] = resultList;

packages/web-speech-cognitive-services/src/SpeechServices/SpeechToText/cognitiveServiceEventResultToWebSpeechRecognitionResult.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import SpeechSDK from '../SpeechSDK';
22

3-
import { type RecognitionResult } from 'microsoft-cognitiveservices-speech-sdk';
43
import SpeechRecognitionAlternative from './SpeechRecognitionAlternative';
54
import SpeechRecognitionResult from './SpeechRecognitionResult';
5+
import type { SerializedRecognitionResult } from './private/serializeRecognitionResult';
66

77
const {
88
ResultReason: { RecognizingSpeech, RecognizedSpeech }
99
} = SpeechSDK;
1010

1111
export default function (
12-
result: RecognitionResult,
12+
result: SerializedRecognitionResult,
1313
init?:
1414
| {
1515
maxAlternatives: number;

0 commit comments

Comments
 (0)