Skip to content

Commit a763097

Browse files
committed
fix(types): use exact optional property types
1 parent 9d33c9f commit a763097

File tree

7 files changed

+29
-21
lines changed

7 files changed

+29
-21
lines changed

package.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {visualizer} from 'rollup-plugin-visualizer'
44
import {name, version} from './package.json'
55

66
export default defineConfig({
7+
tsconfig: './tsconfig.build.json',
8+
79
extract: {
810
rules: {
911
'tsdoc-undefined-tag': 'off',

src/errors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ export class ParseError extends Error {
1818
/**
1919
* In the case of an unknown field encountered in the stream, this will be the field name.
2020
*/
21-
field?: string
21+
field?: string | undefined
2222

2323
/**
2424
* In the case of an unknown field encountered in the stream, this will be the value of the field.
2525
*/
26-
value?: string
26+
value?: string | undefined
2727

2828
/**
2929
* The line that caused the error, if available.
3030
*/
31-
line?: string
31+
line?: string | undefined
3232

3333
constructor(
3434
message: string,

src/stream.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ export interface StreamOptions {
1616
*
1717
* @defaultValue `undefined`
1818
*/
19-
onError?: 'terminate' | ((error: Error) => void)
19+
onError?: ('terminate' | ((error: Error) => void)) | undefined
2020

2121
/**
2222
* Callback for when a reconnection interval is sent from the server.
2323
*
2424
* @param retry - The number of milliseconds to wait before reconnecting.
2525
*/
26-
onRetry?: (retry: number) => void
26+
onRetry?: ((retry: number) => void) | undefined
2727

2828
/**
2929
* Callback for when a comment is encountered in the stream.
3030
*
3131
* @param comment - The comment encountered in the stream.
3232
*/
33-
onComment?: (comment: string) => void
33+
onComment?: ((comment: string) => void) | undefined
3434
}
3535

3636
/**

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface EventSourceParser {
1111
/**
1212
* Feeds the parser another chunk. The method _does not_ return a parsed message.
1313
* Instead, callbacks passed when creating the parser will be triggered once we see enough data
14-
* for a valid/invalid parsing step (see @link {ParserCallbacks}).
14+
* for a valid/invalid parsing step (see {@link ParserCallbacks}).
1515
*
1616
* @param chunk - The chunk to parse. Can be a partial, eg in the case of streaming messages.
1717
* @public
@@ -43,13 +43,13 @@ export interface EventSourceMessage {
4343
* implementation in that browsers will default this to `message`, whereas this parser will
4444
* leave this as `undefined` if not explicitly declared.
4545
*/
46-
event?: string
46+
event?: string | undefined
4747

4848
/**
4949
* ID of the message, if any was provided by the server. Can be used by clients to keep the
5050
* last received message ID in sync when reconnecting.
5151
*/
52-
id?: string
52+
id?: string | undefined
5353

5454
/**
5555
* The data received for this message
@@ -70,21 +70,21 @@ export interface ParserCallbacks {
7070
*
7171
* @param event - The parsed event/message
7272
*/
73-
onEvent?: (event: EventSourceMessage) => void
73+
onEvent?: ((event: EventSourceMessage) => void) | undefined
7474

7575
/**
7676
* Callback for when the server sends a new reconnection interval through the `retry` field.
7777
*
7878
* @param retry - The number of milliseconds to wait before reconnecting.
7979
*/
80-
onRetry?: (retry: number) => void
80+
onRetry?: ((retry: number) => void) | undefined
8181

8282
/**
8383
* Callback for when a comment is encountered in the stream.
8484
*
8585
* @param comment - The comment encountered in the stream.
8686
*/
87-
onComment?: (comment: string) => void
87+
onComment?: ((comment: string) => void) | undefined
8888

8989
/**
9090
* Callback for when an error occurs during parsing. This is a catch-all for any errors
@@ -93,5 +93,5 @@ export interface ParserCallbacks {
9393
*
9494
* @param error - The error that occurred during parsing
9595
*/
96-
onError?: (error: ParseError) => void
96+
onError?: ((error: ParseError) => void) | undefined
9797
}

test/mock.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import {expect, vi} from 'vitest'
33
import type {EventSourceMessage, ParserCallbacks} from '../src/types.ts'
44

55
interface MessageMatcher {
6-
id?: string | RegExp
7-
event?: string
8-
data?: string | RegExp
6+
id?: string | RegExp | undefined
7+
event?: string | undefined
8+
data?: string | RegExp | undefined
99
}
1010

1111
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types

test/parse.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,15 +325,15 @@ test('stream with huge data chunks', async () => {
325325
const parser = createParser({onEvent: mock.onParse})
326326
await getHugeMessageFixtureStream(parser.feed)
327327
const hugeMsg = mock.events[0]
328-
if (hugeMsg.type !== 'event') {
328+
if (!hugeMsg || hugeMsg.type !== 'event') {
329329
throw new Error('First message was not an event')
330330
}
331331

332332
expect(hugeMsg.data.length).toBe(4808512)
333333

334334
const receivedHash = createHash('sha256').update(hugeMsg.data).digest('hex')
335335
const hashMsg = mock.events[1]
336-
if (hashMsg.type !== 'event') {
336+
if (!hashMsg || hashMsg.type !== 'event') {
337337
throw new Error('Second message was not an event')
338338
}
339339

@@ -387,7 +387,7 @@ test('calls onError when the stream is invalid but not newline-terminated (throu
387387
expect(onEvent).not.toHaveBeenCalled()
388388
expect(onError).toHaveBeenCalled()
389389

390-
const error = onError.mock.calls[0][0]
390+
const error = onError.mock.calls[0]?.[0]
391391
expect(error).toBeInstanceOf(ParseError)
392392
expect(error).toMatchObject({
393393
type: 'unknown-field',
@@ -411,7 +411,7 @@ test('calls onError when the stream is invalid (through newline)', async () => {
411411
expect(onEvent).not.toHaveBeenCalled()
412412
expect(onError).toHaveBeenCalled()
413413

414-
const error = onError.mock.calls[0][0]
414+
const error = onError.mock.calls[0]?.[0]
415415
expect(error).toBeInstanceOf(ParseError)
416416
expect(error).toMatchObject({
417417
type: 'unknown-field',
@@ -430,7 +430,7 @@ test('calls onError when the stream is invalid (no field separator)', async () =
430430
expect(onEvent).not.toHaveBeenCalled()
431431
expect(onError).toHaveBeenCalled()
432432

433-
const error = onError.mock.calls[0][0]
433+
const error = onError.mock.calls[0]?.[0]
434434
expect(error).toBeInstanceOf(ParseError)
435435
expect(error).toMatchObject({
436436
type: 'unknown-field',

tsconfig.build.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"noCheck": true
5+
}
6+
}

0 commit comments

Comments
 (0)