diff --git a/lib/modules/types.js b/lib/modules/types.js index 64418616..e173f73b 100755 --- a/lib/modules/types.js +++ b/lib/modules/types.js @@ -15,6 +15,8 @@ const Utils = require('../utils'); const internals = { compiler: { strict: true, + noUncheckedIndexedAccess: true, + exactOptionalPropertyTypes: true, jsx: Ts.JsxEmit.React, lib: ['lib.es2020.d.ts'], module: Ts.ModuleKind.CommonJS, diff --git a/package.json b/package.json index 969bc281..120a22d5 100755 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ }, "peerDependencies": { "@hapi/eslint-plugin": "^7.0.0", - "typescript": ">=3.6.5" + "typescript": ">=4.4.0" }, "peerDependenciesMeta": { "typescript": { diff --git a/test/types.js b/test/types.js index b3ee5538..48fc376a 100755 --- a/test/types.js +++ b/test/types.js @@ -98,6 +98,18 @@ describe('Types', () => { line: 3, column: 4 }, + { + filename: 'test/restrict.ts', + message: `Type 'undefined' is not assignable to type 'string' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the type of the target.`, + line: 10, + column: 0 + }, + { + filename: 'test/restrict.ts', + message: `'unchecked.b' is possibly 'undefined'.`, + line: 15, + column: 0 + }, { filename: 'test/syntax.ts', message: `')' expected.`, diff --git a/test/types/errors/lib/index.d.ts b/test/types/errors/lib/index.d.ts index 42453aa8..bb5db30d 100755 --- a/test/types/errors/lib/index.d.ts +++ b/test/types/errors/lib/index.d.ts @@ -8,3 +8,14 @@ export default add; export const sample: { readonly x: string }; export function hasProperty(property: { name: string }): boolean; + +export interface UsesExactOptionalPropertyTypes { + a?: boolean | undefined; + b?: string; +} + +export interface UncheckedIndexedAccess { + a: UsesExactOptionalPropertyTypes; + + [prop: string]: UsesExactOptionalPropertyTypes; +} diff --git a/test/types/errors/test/restrict.ts b/test/types/errors/test/restrict.ts new file mode 100644 index 00000000..4705a9d0 --- /dev/null +++ b/test/types/errors/test/restrict.ts @@ -0,0 +1,15 @@ +import * as Lab from '../../../..'; +import type { UncheckedIndexedAccess, UsesExactOptionalPropertyTypes } from '../lib/index'; + +const { expect } = Lab.types; + +const exact: UsesExactOptionalPropertyTypes = { a: true }; + +exact.a = undefined; +exact.b = 'ok'; +exact.b = undefined; // Fails + +const unchecked: UncheckedIndexedAccess = { a: exact, b: {} }; + +unchecked.a.a; +unchecked.b.a; // Fails