Skip to content

Commit ada1e42

Browse files
authored
Tighten type checking config (#1071)
* Use more restrictive type signatures * Don't rely on broken expect.error()
1 parent 0c55843 commit ada1e42

File tree

5 files changed

+41
-1
lines changed

5 files changed

+41
-1
lines changed

lib/modules/types.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const Utils = require('../utils');
1515
const internals = {
1616
compiler: {
1717
strict: true,
18+
noUncheckedIndexedAccess: true,
19+
exactOptionalPropertyTypes: true,
1820
jsx: Ts.JsxEmit.React,
1921
lib: ['lib.es2020.d.ts'],
2022
module: Ts.ModuleKind.CommonJS,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
},
3434
"peerDependencies": {
3535
"@hapi/eslint-plugin": "^7.0.0",
36-
"typescript": ">=3.6.5"
36+
"typescript": ">=4.4.0"
3737
},
3838
"peerDependenciesMeta": {
3939
"typescript": {

test/types.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,18 @@ describe('Types', () => {
9898
line: 3,
9999
column: 4
100100
},
101+
{
102+
filename: 'test/restrict.ts',
103+
message: `Type 'undefined' is not assignable to type 'string' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the type of the target.`,
104+
line: 10,
105+
column: 0
106+
},
107+
{
108+
filename: 'test/restrict.ts',
109+
message: `'unchecked.b' is possibly 'undefined'.`,
110+
line: 15,
111+
column: 0
112+
},
101113
{
102114
filename: 'test/syntax.ts',
103115
message: `')' expected.`,

test/types/errors/lib/index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,14 @@ export default add;
88
export const sample: { readonly x: string };
99

1010
export function hasProperty(property: { name: string }): boolean;
11+
12+
export interface UsesExactOptionalPropertyTypes {
13+
a?: boolean | undefined;
14+
b?: string;
15+
}
16+
17+
export interface UncheckedIndexedAccess {
18+
a: UsesExactOptionalPropertyTypes;
19+
20+
[prop: string]: UsesExactOptionalPropertyTypes;
21+
}

test/types/errors/test/restrict.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as Lab from '../../../..';
2+
import type { UncheckedIndexedAccess, UsesExactOptionalPropertyTypes } from '../lib/index';
3+
4+
const { expect } = Lab.types;
5+
6+
const exact: UsesExactOptionalPropertyTypes = { a: true };
7+
8+
exact.a = undefined;
9+
exact.b = 'ok';
10+
exact.b = undefined; // Fails
11+
12+
const unchecked: UncheckedIndexedAccess = { a: exact, b: {} };
13+
14+
unchecked.a.a;
15+
unchecked.b.a; // Fails

0 commit comments

Comments
 (0)