EDITOR: Enable strict typechecking of typescript. (#1493)

* EDITOR: Tweak typescript language server configuration to match use.

- We allow importing files with `.ts`/`.tsx` extensions.
- We use an file-at-a-time transpiler, so we don't support features
  that require understanding the full type system.
- We use the classic `React.createElement` transform.
This commit is contained in:
Tom Prince 2024-07-23 22:28:05 -06:00 committed by GitHub
parent 854c1a5921
commit 02538d6953
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -63,26 +63,45 @@ export class ScriptEditor {
// Add ts definitions for API // Add ts definitions for API
const source = netscriptDefinitions.replace(/export /g, ""); const source = netscriptDefinitions.replace(/export /g, "");
for (const languageDefaults of [ for (const [language, languageDefaults] of [
monaco.languages.typescript.javascriptDefaults, ["javascript", monaco.languages.typescript.javascriptDefaults],
monaco.languages.typescript.typescriptDefaults, ["typescript", monaco.languages.typescript.typescriptDefaults],
]) { ] as const) {
languageDefaults.addExtraLib(source, "netscript.d.ts"); languageDefaults.addExtraLib(source, "netscript.d.ts");
languageDefaults.addExtraLib(reactTypes, "react.d.ts"); languageDefaults.addExtraLib(reactTypes, "react.d.ts");
languageDefaults.addExtraLib(reactDomTypes, "react-dom.d.ts"); languageDefaults.addExtraLib(reactDomTypes, "react-dom.d.ts");
} languageDefaults.setCompilerOptions({
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({ ...languageDefaults.getCompilerOptions(),
...monaco.languages.typescript.typescriptDefaults.getCompilerOptions(), // We allow direct importing of `.ts`/`.tsx` files, so tell the typescript language server that.
jsx: monaco.languages.typescript.JsxEmit.ReactJSX, allowImportingTsExtensions: true,
// We use file-at-a-time transpiler. See https://www.typescriptlang.org/tsconfig/#isolatedModules
isolatedModules: true,
// We use the classic (i.e. `React.createElement`:) react runtime.
jsx: monaco.languages.typescript.JsxEmit.React,
// We define `React` and `ReactDOM` as globals. Don't mark using them as errors.
allowUmdGlobalAccess: true, allowUmdGlobalAccess: true,
// Enable strict typechecking.
// Note that checking in javascript is disabled by default but can be enabled via `// @ts-check`.
// This enables strictNullChecks, which impacts reported types, even in javascript.
strict: true,
noImplicitAny: language === "typescript",
noImplicitReturns: true,
}); });
/** languageDefaults.setDiagnosticsOptions({
* Ignore these errors in the editor: ...languageDefaults.getDiagnosticsOptions(),
* - Cannot find module ''. Did you mean to set the 'moduleResolution' option to 'nodenext', or to add aliases to the 'paths' option?(2792) // Show semantic errors, even in javascript.
*/ // Note that this will only happen if checking is enabled in javascript (e.g. by `// @ts-check`)
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({ noSemanticValidation: false,
diagnosticCodesToIgnore: [2792], // Ignore these errors in the editor:
diagnosticCodesToIgnore: [
// We define `React` and `ReactDOM` as globals. Don't mark using them as errors.
// Even though we set allowUmdGlobalAccess, it still shows a warning (instead of an error).
// - 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead.(2686)
2686,
],
}); });
}
monaco.languages.json.jsonDefaults.setModeConfiguration({ monaco.languages.json.jsonDefaults.setModeConfiguration({
...monaco.languages.json.jsonDefaults.modeConfiguration, ...monaco.languages.json.jsonDefaults.modeConfiguration,
//completion should be disabled because the //completion should be disabled because the