diff --git a/packages/website/src/components/config/ConfigTypeScript.tsx b/packages/website/src/components/config/ConfigTypeScript.tsx index 27cc2cbdd97b..b2c215c6b577 100644 --- a/packages/website/src/components/config/ConfigTypeScript.tsx +++ b/packages/website/src/components/config/ConfigTypeScript.tsx @@ -1,7 +1,6 @@ -import React, { useCallback } from 'react'; -import tsConfigOptions from '../tsConfigOptions.json'; +import React, { useCallback, useEffect, useState } from 'react'; -import ConfigEditor from './ConfigEditor'; +import ConfigEditor, { ConfigOptionsType } from './ConfigEditor'; import type { CompilerFlags } from '../types'; import { shallowEqual } from '../lib/shallowEqual'; @@ -11,11 +10,59 @@ interface ModalTypeScriptProps { readonly config?: CompilerFlags; } +interface OptionDeclarations { + name: string; + type?: unknown; + category?: { message: string }; + description?: { message: string }; +} + function checkOptions(item: [string, unknown]): item is [string, boolean] { return typeof item[1] === 'boolean'; } function ConfigTypeScript(props: ModalTypeScriptProps): JSX.Element { + const [tsConfigOptions, updateOptions] = useState([]); + + useEffect(() => { + if (window.ts) { + updateOptions( + Object.values( + // @ts-expect-error: definition is not fully correct + (window.ts.optionDeclarations as OptionDeclarations[]) + .filter( + item => + item.type === 'boolean' && + item.description && + item.category && + ![ + 'Command-line Options', + 'Modules', + 'Projects', + 'Compiler Diagnostics', + 'Editor Support', + 'Output Formatting', + 'Watch and Build Modes', + 'Source Map Options', + ].includes(item.category.message), + ) + .reduce>((group, item) => { + const category = item.category!.message; + group[category] = group[category] ?? { + heading: category, + fields: [], + }; + group[category].fields.push({ + key: item.name, + label: item.description!.message, + }); + return group; + }, {}), + ), + ); + } + }, [props.isOpen]); + const onClose = useCallback( (newConfig: Record) => { const cfg = Object.fromEntries( diff --git a/packages/website/src/components/editor/LoadedEditor.tsx b/packages/website/src/components/editor/LoadedEditor.tsx index a3e8395bc3d7..98560ee4893a 100644 --- a/packages/website/src/components/editor/LoadedEditor.tsx +++ b/packages/website/src/components/editor/LoadedEditor.tsx @@ -139,8 +139,13 @@ export const LoadedEditor: React.FC = ({ useEffect(() => { sandboxInstance.setCompilerSettings({ + noResolve: true, + strict: true, + target: main.languages.typescript.ScriptTarget.ESNext, + module: main.languages.typescript.ModuleKind.ESNext, + lib: ['ESNext'], ...tsConfig, - jsx: jsx ? 2 : 0, + jsx: jsx ? main.languages.typescript.JsxEmit.React : undefined, }); }, [jsx, sandboxInstance, JSON.stringify(tsConfig) /* todo: better way? */]); diff --git a/packages/website/src/components/tsConfigOptions.json b/packages/website/src/components/tsConfigOptions.json deleted file mode 100644 index 8ddd3b6d35df..000000000000 --- a/packages/website/src/components/tsConfigOptions.json +++ /dev/null @@ -1,97 +0,0 @@ -[ - { - "heading": "Interop Constraints", - "fields": [ - { - "key": "isolatedModules", - "label": "Ensure that each file can be safely transpiled without relying on other imports." - }, - { - "key": "allowSyntheticDefaultImports", - "label": "Allow `import x from y` when a module doesn't have a default export." - }, - { - "key": "esModuleInterop", - "label": "Emit additional JavaScript to ease support for importing CommonJS modules. This enables allowSyntheticDefaultImports for type compatibility." - } - ] - }, - { - "heading": "Type Checking", - "fields": [ - { - "key": "strict", - "label": "Enable all strict type-checking options." - }, - { - "key": "noImplicitAny", - "label": "Enable error reporting for expressions and declarations with an implied any type.." - }, - { - "key": "strictNullChecks", - "label": "When type checking, take into account null and undefined." - }, - { - "key": "strictFunctionTypes", - "label": "When assigning functions, check to ensure parameters and the return values are subtype-compatible." - }, - { - "key": "strictBindCallApply", - "label": "Check that the arguments for bind, call, and apply methods match the original function." - }, - { - "key": "strictPropertyInitialization", - "label": "Check for class properties that are declared but not set in the constructor." - }, - { - "key": "noImplicitThis", - "label": "Enable error reporting when this is given the type any." - }, - { - "key": "alwaysStrict", - "label": "Ensure `use strict` is always emitted." - }, - { - "key": "noUnusedLocals", - "label": "Enable error reporting when a local variables aren't read." - }, - { - "key": "noUnusedParameters", - "label": "Raise an error when a function parameter isn't read." - }, - { - "key": "noImplicitReturns", - "label": "Enable error reporting for codepaths that do not explicitly return in a function." - }, - { - "key": "noFallthroughCasesInSwitch", - "label": "Enable error reporting for fallthrough cases in switch statements." - }, - { - "key": "allowUnusedLabels", - "label": "Disable error reporting for unused labels." - }, - { - "key": "allowUnreachableCode", - "label": "Disable error reporting for unreachable code." - } - ] - }, - { - "heading": "Language and Environment", - "fields": [ - { - "key": "experimentalDecorators", - "label": "Enable experimental support for TC39 stage 2 draft decorators." - }, - { - "key": "emitDecoratorMetadata", - "label": "Emit design-type metadata for decorated declarations in source files." - }, - { - "key": "noLib", - "label": "Disable including any library files, including the default lib.d.ts." - } - ] - } -]