Skip to content

Commit

Permalink
Tighten type checking config (#1071)
Browse files Browse the repository at this point in the history
* Use more restrictive type signatures
* Don't rely on broken expect.error()
  • Loading branch information
kanongil authored Oct 22, 2024
1 parent 0c55843 commit ada1e42
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/modules/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"peerDependencies": {
"@hapi/eslint-plugin": "^7.0.0",
"typescript": ">=3.6.5"
"typescript": ">=4.4.0"
},
"peerDependenciesMeta": {
"typescript": {
Expand Down
12 changes: 12 additions & 0 deletions test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
Expand Down
11 changes: 11 additions & 0 deletions test/types/errors/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
15 changes: 15 additions & 0 deletions test/types/errors/test/restrict.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ada1e42

Please sign in to comment.