From 4902a1cacb0348efde9ab342172f2706ac27e837 Mon Sep 17 00:00:00 2001 From: Ry Dahl Date: Thu, 14 Nov 2019 15:05:36 -0500 Subject: [PATCH] Turn on TS strict mode for deno_typescript (#3330) --- cli/js/body.ts | 3 ++- cli/js/compiler.ts | 4 +++- cli/js/lib.deno_runtime.d.ts | 8 +++++++- cli/js/os.ts | 3 ++- cli/js/streams/readable-internals.ts | 2 +- cli/js/url.ts | 2 +- deno_typescript/lib.deno_core.d.ts | 2 +- deno_typescript/lib.rs | 1 + std/node/fs.ts | 11 +++++++++-- 9 files changed, 27 insertions(+), 9 deletions(-) diff --git a/cli/js/body.ts b/cli/js/body.ts index fc12efa8..ed21fa0e 100644 --- a/cli/js/body.ts +++ b/cli/js/body.ts @@ -143,9 +143,10 @@ export class Body implements domTypes.Body { this._stream = this._bodySource; } if (typeof this._bodySource === "string") { + const bodySource = this._bodySource; this._stream = new ReadableStream({ start(controller: ReadableStreamController): void { - controller.enqueue(this._bodySource); + controller.enqueue(bodySource); controller.close(); } }); diff --git a/cli/js/compiler.ts b/cli/js/compiler.ts index 775277cd..1779f76c 100644 --- a/cli/js/compiler.ts +++ b/cli/js/compiler.ts @@ -285,7 +285,7 @@ async function processImports( referrer = "" ): Promise { if (!specifiers.length) { - return; + return []; } const sources = specifiers.map(([, moduleSpecifier]) => moduleSpecifier); const sourceFiles = await fetchSourceFiles(sources, referrer); @@ -385,6 +385,8 @@ class Host implements ts.CompilerHost { private readonly _options: ts.CompilerOptions = { allowJs: true, allowNonTsExtensions: true, + // TODO(#3324) Enable strict mode for user code. + // strict: true, checkJs: false, esModuleInterop: true, module: ts.ModuleKind.ESNext, diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts index 87be1c3a..c7c40398 100644 --- a/cli/js/lib.deno_runtime.d.ts +++ b/cli/js/lib.deno_runtime.d.ts @@ -2858,4 +2858,10 @@ declare namespace WebAssembly { } } -/* eslint-enable @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any */ +// Catch-all for JSX elements. +// See https://www.typescriptlang.org/docs/handbook/jsx.html#intrinsic-elements +declare namespace JSX { + interface IntrinsicElements { + [elemName: string]: any; + } +} diff --git a/cli/js/os.ts b/cli/js/os.ts index 89e5652c..4e17e203 100644 --- a/cli/js/os.ts +++ b/cli/js/os.ts @@ -95,7 +95,8 @@ export function start(preserveDenoNamespace = true, source?: string): Start { for (const [name, opId] of Object.entries(ops)) { const opName = `OP_${name.toUpperCase()}`; // Assign op ids to actual variables - dispatch[opName] = opId; + // TODO(ry) This type casting is gross and should be fixed. + ((dispatch as unknown) as { [key: string]: number })[opName] = opId; } // First we send an empty `Start` message to let the privileged side know we // are ready. The response should be a `StartRes` message containing the CLI diff --git a/cli/js/streams/readable-internals.ts b/cli/js/streams/readable-internals.ts index 36f4223d..67f5a69b 100644 --- a/cli/js/streams/readable-internals.ts +++ b/cli/js/streams/readable-internals.ts @@ -259,7 +259,7 @@ export function isReadableStreamLocked( export function readableStreamGetNumReadIntoRequests( stream: SDReadableStream -): number | undefined { +): number { // TODO remove the "as unknown" cast // This is in to workaround a compiler error // error TS2352: Conversion of type 'SDReadableStreamReader' to type 'SDReadableStreamBYOBReader' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. diff --git a/cli/js/url.ts b/cli/js/url.ts index 2017bb40..489d8d04 100644 --- a/cli/js/url.ts +++ b/cli/js/url.ts @@ -156,7 +156,7 @@ export class URL { "search" ]; const objectString = keys - .map((key: string) => `${key}: "${this[key] || ""}"`) + .map((key: string) => `${key}: "${this[key as keyof this] || ""}"`) .join(", "); return `URL { ${objectString} }`; } diff --git a/deno_typescript/lib.deno_core.d.ts b/deno_typescript/lib.deno_core.d.ts index c3a20eb0..54966554 100644 --- a/deno_typescript/lib.deno_core.d.ts +++ b/deno_typescript/lib.deno_core.d.ts @@ -21,7 +21,7 @@ interface EvalErrorInfo { } declare interface DenoCore { - print(s: string, isErr?: boolean); + print(s: string, isErr?: boolean): void; dispatch( opId: number, control: Uint8Array, diff --git a/deno_typescript/lib.rs b/deno_typescript/lib.rs index 56072ebd..e53a4243 100644 --- a/deno_typescript/lib.rs +++ b/deno_typescript/lib.rs @@ -127,6 +127,7 @@ pub fn compile_bundle( let config_json = serde_json::json!({ "compilerOptions": { + "strict": true, "declaration": true, "lib": ["esnext"], "module": "amd", diff --git a/std/node/fs.ts b/std/node/fs.ts index 01b9e239..539916c9 100644 --- a/std/node/fs.ts +++ b/std/node/fs.ts @@ -1,7 +1,14 @@ -import { notImplemented, intoCallbackAPIWithIntercept } from "./_utils.ts"; +import { + notImplemented, + intoCallbackAPIWithIntercept, + MaybeEmpty +} from "./_utils.ts"; const { readFile: denoReadFile, readFileSync: denoReadFileSync } = Deno; -type ReadFileCallback = (err: Error | null, data: string | Uint8Array) => void; +type ReadFileCallback = ( + err: MaybeEmpty, + data: MaybeEmpty +) => void; interface ReadFileOptions { encoding?: string | null; -- GitLab