diff --git a/__tests__/type-external.ts b/__tests__/type-external.ts index fcf3b2df..496a5557 100644 --- a/__tests__/type-external.ts +++ b/__tests__/type-external.ts @@ -1,6 +1,22 @@ import {isType, JSONArray, JSONObject, JSONTypes} from "type-plus" import {Draft} from "../src/types/types-external" import {createDraft, current, original} from "../src/immer" +import ts from "typescript" + +test("source compiles with noUncheckedIndexedAccess (#1143)", () => { + const {config, error} = ts.readConfigFile("tsconfig.json", ts.sys.readFile) + expect(error).toBeUndefined() + const parsed = ts.parseJsonConfigFileContent(config, ts.sys, ".", { + noUncheckedIndexedAccess: true, + skipLibCheck: true + }) + const program = ts.createProgram(parsed.fileNames, parsed.options) + expect( + [...parsed.errors, ...ts.getPreEmitDiagnostics(program)].map(diagnostic => + ts.flattenDiagnosticMessageText(diagnostic.messageText, "\n") + ) + ).toEqual([]) +}) describe("Draft", () => { test("can use JSONTypes as T", () => { diff --git a/src/core/immerClass.ts b/src/core/immerClass.ts index 2922f612..224604b1 100644 --- a/src/core/immerClass.ts +++ b/src/core/immerClass.ts @@ -207,7 +207,7 @@ export class Immer implements ProducersFns { // before applying patches let i: number for (i = patches.length - 1; i >= 0; i--) { - const patch = patches[i] + const patch = patches[i]! if (patch.path.length === 0 && patch.op === "replace") { base = patch.value break diff --git a/src/plugins/patches.ts b/src/plugins/patches.ts index 08c7d0f6..b3c86f86 100644 --- a/src/plugins/patches.ts +++ b/src/plugins/patches.ts @@ -116,7 +116,7 @@ export function enablePatches() { function resolvePath(base: any, path: PatchPath): any { let current = base for (let i = 0; i < path.length - 1; i++) { - const key = path[i] + const key = path[i]! current = get(current, key) if (!isObjectish(current) || current === null) { throw new Error(`Cannot resolve path at '${path.join("/")}'`) @@ -351,7 +351,7 @@ export function enablePatches() { const type = getArchtype(base) const value = deepClonePatchValue(patch.value) // used to clone patch to ensure original patch is not modified, see #411 - const key = path[path.length - 1] + const key = path[path.length - 1]! switch (op) { case REPLACE: switch (type) { diff --git a/src/utils/common.ts b/src/utils/common.ts index 513c153e..43f35ec0 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -215,7 +215,7 @@ export function shallowCopy(base: any, strict: StrictMode) { let keys = Reflect.ownKeys(descriptors) for (let i = 0; i < keys.length; i++) { const key: any = keys[i] - const desc = descriptors[key] + const desc = descriptors[key]! if (desc[WRITABLE] === false) { desc[WRITABLE] = true desc[CONFIGURABLE] = true