Skip to content

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

fix: ensure no type errors exists in users panel

bautistaaa99681058b856
diff --git a/apps/web/src/app/[locale]/wizard/_components/TestCasesEditor.tsx b/apps/web/src/app/[locale]/wizard/_components/TestCasesEditor.tsx
index 85453595..ff6ac3c4 100644
--- a/apps/web/src/app/[locale]/wizard/_components/TestCasesEditor.tsx
+++ b/apps/web/src/app/[locale]/wizard/_components/TestCasesEditor.tsx
@@ -1,15 +1,15 @@
import type { OnChange } from '@monaco-editor/react';
import type { TsErrors } from '@repo/monaco';
-import { CodeEditor, loadCheckingLib, type CodeEditorProps } from '@repo/monaco/code-editor';
+import { CodeEditor, type CodeEditorProps } from '@repo/monaco/code-editor';
+import { PrettierFormatProvider } from '@repo/monaco/prettier';
+import { createTwoslashInlayProvider } from '@repo/monaco/twoslash';
import { FormField, FormItem, FormMessage } from '@repo/ui/components/form';
import { TypographyH3 } from '@repo/ui/components/typography/h3';
-import { useCallback, useState } from 'react';
import type * as monaco from 'monaco-editor';
import dynamic from 'next/dynamic';
-import { createTwoslashInlayProvider } from '@repo/monaco/twoslash';
-import { PrettierFormatProvider } from '@repo/monaco/prettier';
-import type { WizardForm } from '.';
+import { useCallback, useState } from 'react';
import { SettingsButton } from '~/app/[locale]/challenge/_components/settings/settings-button';
+import type { WizardForm } from '.';
const VimStatusBar = dynamic(() => import('@repo/monaco/vim-mode'), {
ssr: false,
@@ -35,8 +35,6 @@ export function TestCasesEditor({ form, hasTsErrors, setTsErrors }: Props) {
monaco.languages.registerDocumentFormattingEditProvider('typescript', PrettierFormatProvider);
- loadCheckingLib(monaco);
-
const model = editor.getModel();
if (!model) {
diff --git a/packages/db/mocks/challenges.mock.ts b/packages/db/mocks/challenges.mock.ts
index acde4ec5..4f16aa30 100644
--- a/packages/db/mocks/challenges.mock.ts
+++ b/packages/db/mocks/challenges.mock.ts
@@ -52,10 +52,7 @@ export async function loadChallengesFromTypeChallenge(isProd = false) {
);
const testData = await readFile(resolve(QUESTIONS_PATH, dir.name, 'test-cases.ts')).then((f) =>
- f
- .toString()
- .replace(/import .* from '@type-challenges\/utils';?/g, '')
- .trim(),
+ f.toString().trim(),
);
const [selfId] = dir.name.split('-');
diff --git a/packages/monaco/src/code-editor.tsx b/packages/monaco/src/code-editor.tsx
index ded0cfae..cb4acf83 100644
--- a/packages/monaco/src/code-editor.tsx
+++ b/packages/monaco/src/code-editor.tsx
@@ -4,9 +4,6 @@ import Editor, { loader, type EditorProps } from '@monaco-editor/react';
import { useTheme } from 'next-themes';
import { useMemo } from 'react';
import { useEditorSettingsStore } from './settings-store';
-import { libSource } from './editor-types';
-// eslint-disable-next-line @typescript-eslint/consistent-type-imports
-import * as MonacoEditor from 'monaco-editor';
const ADMIN_HOST = 'admin.typehero.dev';
const getBaseUrl = () => {
@@ -40,15 +37,6 @@ const DEFAULT_OPTIONS = {
fontSize: 16,
} as const satisfies EditorProps['options'];
-export const LIB_URI = 'file:///asserts.d.ts';
-
-export function loadCheckingLib(monaco: typeof MonacoEditor) {
- if (!monaco.editor.getModel(monaco.Uri.parse(LIB_URI))) {
- monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, LIB_URI);
- monaco.editor.createModel(libSource, 'typescript', monaco.Uri.parse(LIB_URI));
- }
-}
-
export type CodeEditorProps = Omit<EditorProps, 'theme'>;
export function CodeEditor({ onChange, onMount, options, value, ...props }: CodeEditorProps) {
diff --git a/packages/monaco/src/editor-types.ts b/packages/monaco/src/editor-types.ts
deleted file mode 100644
index 0b8e948d..00000000
--- a/packages/monaco/src/editor-types.ts
+++ /dev/null
@@ -1,80 +0,0 @@
-/**
- * These types are used in the editor instance.
- * Essentially, utility types to help test / debug code
- * based on: https://github.com/type-challenges/type-challenges/blob/main/utils/index.d.ts
- */
-export const libSource = `
-type Expect<T extends true> = T;
-type ExpectTrue<T extends true> = T;
-type ExpectFalse<T extends false> = T;
-type IsTrue<T extends true> = T;
-type IsFalse<T extends false> = T;
-
-type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true;
-type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
- ? 1
- : 2
- ? true
- : false;
-
-type NotAny<T> = true extends IsAny<T> ? false : true;
-
-type Debug<T> = { [K in keyof T]: T[K] };
-type MergeInsertions<T> = T extends object
- ? { [K in keyof T]: MergeInsertions<T[K]> }
- : T;
-
-type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>;
-
-type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false;
-type ExpectValidArgs<
- FUNC extends (...args: any[]) => any,
- ARGS extends any[]
-> = ARGS extends Parameters<FUNC> ? true : false;
-
-type NoInfer<T> = [T][T extends any ? 0 : never];
-
-declare function id<T>(): T;
-
-/**
- * Helper function to test your code.
- *
- * Checks if two types have all the same properties and modifiers
- *
- * To check what it is failing, you can hover over \`Equal\` and see what the types failing are
- */
-declare function Equal<A, B>(
- ...args: [
- ...([a: A, b: B] | []),
- ...(Alike<A, B> extends true ? [] : [msg: "not equal"])
- ]
-): void;
-
-/**
- * Helper function to test your code.
- *
- * Checks that two types aren't the same
- *
- * To check what it is failing, you can hover over \`NotEqual\` and see what the types failing are
- */
-declare function NotEqual<A, B>(
- ...args: [
- ...([a: A, b: B] | []),
- ...(Alike<A, B> extends true ? [msg: "equal"] : [])
- ]
-): void;
-
-/**
- * Helper function to test your code.
- *
- * Checks if type \`A\` extends \`B\`
- *
- * To check what it is failing, you can hover over \`Extends\` and see what the types failing are
- */
-declare function Extends<A, B>(
- ...args: [
- ...([a: A, b: B] | []),
- ...([A] extends [B] ? [] : [msg: [A, "doesn't extend", B]])
- ]
-): void;
-`;
diff --git a/packages/monaco/src/index.tsx b/packages/monaco/src/index.tsx
index 7a367cab..b902d6a3 100644
--- a/packages/monaco/src/index.tsx
+++ b/packages/monaco/src/index.tsx
@@ -98,6 +98,7 @@ export function CodePanel(props: CodePanelProps) {
}
};
const hasFailingTest = tsErrors?.some((e) => e.length) ?? false;
+ console.count('CodePanel');
return (
<>
@@ -120,13 +121,24 @@ export function CodePanel(props: CodePanelProps) {
if (!model) return null;
const tsWorker = await getTsWorker(model.uri);
- const errors = await Promise.all([
+ const testErrors = await Promise.all([
tsWorker.getSemanticDiagnostics(TESTS_PATH),
tsWorker.getSyntacticDiagnostics(TESTS_PATH),
tsWorker.getCompilerOptionsDiagnostics(TESTS_PATH),
] as const);
- setTsErrors(errors);
+ const userErrors = await Promise.all([
+ tsWorker.getSemanticDiagnostics(USER_CODE_PATH),
+ tsWorker.getSyntacticDiagnostics(USER_CODE_PATH),
+ tsWorker.getCompilerOptionsDiagnostics(USER_CODE_PATH),
+ ] as const);
+
+ setTsErrors(
+ testErrors.map((err, i) => {
+ return [...err, ...(userErrors[i] || [])];
+ }) as TsErrors,
+ );
+
setTestEditorState(editor);
},
user: async (editor, monaco) => {
@@ -148,7 +160,17 @@ export function CodePanel(props: CodePanelProps) {
tsWorker.getCompilerOptionsDiagnostics(USER_CODE_PATH),
] as const);
- setTsErrors(testErrors);
+ const userErrors = await Promise.all([
+ tsWorker.getSemanticDiagnostics(USER_CODE_PATH),
+ tsWorker.getSyntacticDiagnostics(USER_CODE_PATH),
+ tsWorker.getCompilerOptionsDiagnostics(USER_CODE_PATH),
+ ] as const);
+
+ setTsErrors(
+ testErrors.map((err, i) => {
+ return [...err, ...(userErrors[i] || [])];
+ }) as TsErrors,
+ );
},
}}
onChange={{
@@ -197,7 +219,17 @@ export function CodePanel(props: CodePanelProps) {
tsWorker.getCompilerOptionsDiagnostics(TESTS_PATH),
] as const);
- setTsErrors(testErrors);
+ const userErrors = await Promise.all([
+ tsWorker.getSemanticDiagnostics(USER_CODE_PATH),
+ tsWorker.getSyntacticDiagnostics(USER_CODE_PATH),
+ tsWorker.getCompilerOptionsDiagnostics(USER_CODE_PATH),
+ ] as const);
+
+ setTsErrors(
+ testErrors.map((err, i) => {
+ return [...err, ...(userErrors[i] || [])];
+ }) as TsErrors,
+ );
},
}}
/>
diff --git a/packages/monaco/src/split-editor.tsx b/packages/monaco/src/split-editor.tsx
index 15ec30dc..0ea056e8 100644
--- a/packages/monaco/src/split-editor.tsx
+++ b/packages/monaco/src/split-editor.tsx
@@ -10,9 +10,8 @@ import type * as monacoType from 'monaco-editor';
import dynamic from 'next/dynamic';
import { useEffect, useRef, useState } from 'react';
import ts from 'typescript';
-import { CodeEditor, LIB_URI } from './code-editor';
+import { CodeEditor } from './code-editor';
import { useResetEditor } from './editor-hooks';
-import { libSource } from './editor-types';
import { PrettierFormatProvider } from './prettier';
import { useEditorSettingsStore } from './settings-store';
import { getEventDeltas } from './utils';
@@ -145,13 +144,15 @@ export default function SplitEditor({
monacoRef.current.editor.createModel(code, 'typescript', uri);
}
- const userCode = monacoRef.current.editor
- .getModel(monacoRef.current.Uri.parse(USER_CODE_PATH))!
- .getValue();
+ const userCode =
+ monacoRef.current.editor
+ .getModel(monacoRef.current.Uri.parse(USER_CODE_PATH))
+ ?.getValue() ?? '';
- const testCode = monacoRef.current.editor
- .getModel(monacoRef.current.Uri.parse(TESTS_PATH))!
- .getValue();
+ const testCode =
+ monacoRef.current.editor
+ .getModel(monacoRef.current.Uri.parse(TESTS_PATH))
+ ?.getValue() ?? '';
if (hasImports(userCode)) {
monacoRef.current.languages.typescript.typescriptDefaults.addExtraLib(
@@ -296,17 +297,8 @@ export default function SplitEditor({
// this just does the typechecking so the UI can update
onMount?.user?.(editor, monaco);
typeCheck(monaco);
- const libUri = monaco.Uri.parse(LIB_URI);
-
monaco.languages.typescript.typescriptDefaults.setEagerModelSync(true);
- if (!monaco.editor.getModel(libUri)) {
- monaco.languages.typescript.javascriptDefaults.addExtraLib(libSource, LIB_URI);
- monaco.editor
- .createModel(libSource, 'typescript', libUri)
- .setEOL(monaco.editor.EndOfLineSequence.LF);
- }
-
const model = monaco.editor.getModel(monaco.Uri.parse(USER_CODE_PATH))!;
const code = model.getValue();
debouncedUserCodeAta(code);