|
| 1 | +/** |
| 2 | + * Compile-time type checks for the Transport interface. |
| 3 | + * |
| 4 | + * Verifies that a class declaring optional Transport properties as `T | undefined` |
| 5 | + * (the pattern required by `exactOptionalPropertyTypes: true`) is assignable to |
| 6 | + * Transport without TS2420 errors when compiled with the dedicated |
| 7 | + * exact-optional typecheck config. |
| 8 | + * |
| 9 | + * See: https://github.com/modelcontextprotocol/typescript-sdk/issues/1314 |
| 10 | + */ |
| 11 | +import { test } from 'vitest'; |
| 12 | + |
| 13 | +import type { Transport } from '../../src/shared/transport.js'; |
| 14 | +import type { JSONRPCMessage, MessageExtraInfo } from '../../src/types/index.js'; |
| 15 | + |
| 16 | +// A concrete class that uses the explicit `| undefined` union form for optional Transport members. |
| 17 | +// With the old Transport interface (no `| undefined` on these members), this class would produce |
| 18 | +// TS2420 under `exactOptionalPropertyTypes: true` when compiled by tsconfig.exact-optional.json. |
| 19 | +class ExplicitUndefinedTransport implements Transport { |
| 20 | + sessionId?: string | undefined; |
| 21 | + onclose?: (() => void) | undefined; |
| 22 | + onerror?: ((error: Error) => void) | undefined; |
| 23 | + onmessage?: (<T extends JSONRPCMessage>(message: T, extra?: MessageExtraInfo) => void) | undefined; |
| 24 | + setProtocolVersion?: ((version: string) => void) | undefined; |
| 25 | + setSupportedProtocolVersions?: ((versions: string[]) => void) | undefined; |
| 26 | + |
| 27 | + async start(): Promise<void> {} |
| 28 | + async close(): Promise<void> {} |
| 29 | + async send(_message: JSONRPCMessage): Promise<void> {} |
| 30 | +} |
| 31 | + |
| 32 | +test('Transport allows explicit | undefined on optional members', () => { |
| 33 | + const transport: Transport = new ExplicitUndefinedTransport(); |
| 34 | + // The mere fact this file compiles is the assertion. |
| 35 | + // We also verify runtime assignability here. |
| 36 | + expect(transport).toBeDefined(); |
| 37 | +}); |
0 commit comments