From 6d393121ab26a3ebfca69f760289d213ec2ad190 Mon Sep 17 00:00:00 2001 From: Dennis Huebner Date: Thu, 30 Nov 2023 14:27:26 +0100 Subject: [PATCH] Improve type checking for notification and request types #14 --- .../vscode-messenger-common/src/messages.ts | 19 +++++++++++++++---- .../webview-ui/src/components/diagram.tsx | 2 +- .../tests/messenger-webview.test.ts | 4 ++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/vscode-messenger-common/src/messages.ts b/packages/vscode-messenger-common/src/messages.ts index 8469154..aa90825 100644 --- a/packages/vscode-messenger-common/src/messages.ts +++ b/packages/vscode-messenger-common/src/messages.ts @@ -141,8 +141,14 @@ export type JsonArray = JsonAny[]; /** * Data structure for defining a request type. */ -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export type RequestType = { method: string }; +export type RequestType = { + method: string + + /** + * Used to ensure correct typing. Clients must not use this property + */ + readonly _?: [P,R] +}; /** * Function for handling incoming requests. @@ -153,8 +159,13 @@ export type HandlerResult = R | Promise; /** * Data structure for defining a notification type. */ -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export type NotificationType

= { method: string }; +export type NotificationType

= { + method: string + /** + * Used to ensure correct typing. Clients must not use this property + */ + readonly _?: P +}; /** * Function for handling incoming notifications. diff --git a/packages/vscode-messenger-devtools/webview-ui/src/components/diagram.tsx b/packages/vscode-messenger-devtools/webview-ui/src/components/diagram.tsx index 1aa8721..27d666c 100644 --- a/packages/vscode-messenger-devtools/webview-ui/src/components/diagram.tsx +++ b/packages/vscode-messenger-devtools/webview-ui/src/components/diagram.tsx @@ -1,4 +1,4 @@ -import React, { useEffect, useRef, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import ForceGraph2D, { ForceGraphMethods, GraphData, LinkObject, NodeObject } from 'react-force-graph-2d'; type GraphObjectExtension = { diff --git a/packages/vscode-messenger-webview/tests/messenger-webview.test.ts b/packages/vscode-messenger-webview/tests/messenger-webview.test.ts index 06a4ce9..56e50f3 100644 --- a/packages/vscode-messenger-webview/tests/messenger-webview.test.ts +++ b/packages/vscode-messenger-webview/tests/messenger-webview.test.ts @@ -199,8 +199,8 @@ describe('Webview Messenger', () => { messenger1.start(); const messenger2 = new Messenger(vsCodeApi); messenger2.start(); - messenger1.sendRequest(stringNotification, HOST_EXTENSION, 'ping1'); - messenger2.sendRequest(stringNotification, HOST_EXTENSION, 'ping2'); + messenger1.sendRequest(stringRequest, HOST_EXTENSION, 'ping1'); + messenger2.sendRequest(stringRequest, HOST_EXTENSION, 'ping2'); const message1 = vsCodeApi.messages[0] as unknown as any; const message2 = vsCodeApi.messages[1] as unknown as any;