Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

added twoslash comments

pythonKiwi1232d4c6b741534
diff --git a/src/components/ui/editor.tsx b/src/components/ui/editor/index.tsx
similarity index 81%
rename from src/components/ui/editor.tsx
rename to src/components/ui/editor/index.tsx
index c701d6b8..d27f674a 100644
--- a/src/components/ui/editor.tsx
+++ b/src/components/ui/editor/index.tsx
@@ -1,10 +1,11 @@
import Editor from '@monaco-editor/react';
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import React from 'react';
+import { createTwoslashInlayProvider } from './twoslash';
declare global {
interface Window {
- editor: any;
+ editor: monaco.editor.IStandaloneCodeEditor;
treeStringToJson: any;
}
}
@@ -22,8 +23,15 @@ export const options: IGlobalEditorOptions | IEditorOptions = {
readOnly: true,
};
-const value = /* set from `myEditor.getModel()`: */ `import { equal } from "checking";
-const r1 = equal<{ a: 1 } & { b: 1 }, { a: 1, b: 1 }>()`;
+const value = `import { equal } from "checking";
+
+const r1 = equal<{ a: 1 } & { b: 1 }, { a: 1, b: 1 }>()
+// ^?
+
+const r2 = equal<{ a: 1 } & { b: 1 }, { a: 2, b: 1 }>()
+// ^?
+`;
+
const libSource = `
declare module "checking" {
/// based on: https://github.com/type-challenges/type-challenges/blob/main/utils/index.d.ts
@@ -88,22 +96,30 @@ export const CodePanel = () => {
monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, libUri);
monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(libUri));
- const worker = await monaco.languages.typescript.getTypeScriptWorker();
- const ts = await worker(editor.getModel()?.uri!);
+ const ts = await (
+ await monaco.languages.typescript.getTypeScriptWorker()
+ )(editor.getModel()?.uri!);
const filename = editor.getModel()?.uri.toString();
// what actually runs when checking errors
const runCommand = async () => {
- console.log(
- ts.getSemanticDiagnostics(filename!), // actual type errors
+ let errors = await Promise.all([
+ ts.getSemanticDiagnostics(filename!),
ts.getSyntacticDiagnostics(filename!),
- // ts.getSuggestionDiagnostics(filename), unused triggers this
+ ts.getSuggestionDiagnostics(filename!),
ts.getCompilerOptionsDiagnostics(filename!),
- );
+ ] as const);
+
+ console.log(errors);
};
editor.getModel()?.onDidChangeContent((x) => runCommand());
+
+ monaco.languages.registerInlayHintsProvider(
+ 'typescript',
+ createTwoslashInlayProvider(monaco, ts),
+ );
};
return (
diff --git a/src/components/ui/editor/twoslash.ts b/src/components/ui/editor/twoslash.ts
new file mode 100644
index 00000000..c0bf5ad0
--- /dev/null
+++ b/src/components/ui/editor/twoslash.ts
@@ -0,0 +1,63 @@
+// based on: https://github.com/microsoft/TypeScript-Website/blob/v2/packages/playground/src/twoslashInlays.ts
+import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
+
+export const createTwoslashInlayProvider = (
+ m: typeof monaco,
+ worker: monaco.languages.typescript.TypeScriptWorker,
+) => {
+ const provider: monaco.languages.InlayHintsProvider = {
+ provideInlayHints: async (model, _, cancel) => {
+ const text = model.getValue();
+ const queryRegex = /^\s*\/\/\s*\^\?$/gm;
+ let match;
+ const results: monaco.languages.InlayHint[] = [];
+ if (model.isDisposed()) {
+ return {
+ hints: [],
+ dispose: () => {},
+ };
+ }
+
+ while ((match = queryRegex.exec(text)) !== null) {
+ const end = match.index + match[0].length - 1;
+ const endPos = model.getPositionAt(end);
+ const inspectionPos = new m.Position(endPos.lineNumber - 1, endPos.column);
+ const inspectionOff = model.getOffsetAt(inspectionPos);
+
+ if (cancel.isCancellationRequested) {
+ return {
+ hints: [],
+ dispose: () => {},
+ };
+ }
+
+ const hint = await worker.getQuickInfoAtPosition(model.uri.toString(), inspectionOff);
+ if (!hint || !hint.displayParts) continue;
+
+ // Make a one-liner
+ let text = hint.displayParts
+ .map((d: { text: string }) => d.text)
+ .join('')
+ .replace(/\r?\n\s*/g, ' ');
+ if (text.length > 120) text = text.slice(0, 119) + '...';
+
+ const inlay: monaco.languages.InlayHint = {
+ // @ts-ignore
+ kind: 0,
+ position: new m.Position(endPos.lineNumber, endPos.column + 1),
+ label: text,
+ paddingLeft: true,
+ };
+
+ results.push(inlay);
+ }
+
+ return {
+ hints: results,
+ dispose: () => {},
+ };
+ },
+ };
+
+ return provider;
+};