Skip to content

Commit 168de40

Browse files
Add missing error codes (#177)
1 parent dfa9bc0 commit 168de40

File tree

9 files changed

+66
-1
lines changed

9 files changed

+66
-1
lines changed

source/lib/compiler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const expectErrordiagnosticCodesToIgnore = new Set<DiagnosticCode>([
3333
DiagnosticCode.ThisContextOfTypeNotAssignableToMethodOfThisType,
3434
DiagnosticCode.ValueOfTypeNotCallable,
3535
DiagnosticCode.ExpressionNotCallable,
36+
DiagnosticCode.TypeNotAssignableWithExactOptionalPropertyTypes,
37+
DiagnosticCode.TypeNotAssignableToParameterWithExactOptionalPropertyTypes,
38+
DiagnosticCode.TypeNotAssignableTypeOfTargetWithExactOptionalPropertyTypes,
39+
DiagnosticCode.IndexSignatureOnlyPermitsReading,
3640
DiagnosticCode.OnlyVoidFunctionIsNewCallable,
3741
DiagnosticCode.ExpressionNotConstructable,
3842
DiagnosticCode.NewExpressionTargetLackingConstructSignatureHasAnyType,

source/lib/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ export enum DiagnosticCode {
3939
ExpressionNotCallable = 2349,
4040
OnlyVoidFunctionIsNewCallable = 2350,
4141
ExpressionNotConstructable = 2351,
42+
TypeNotAssignableWithExactOptionalPropertyTypes = 2375,
43+
TypeNotAssignableToParameterWithExactOptionalPropertyTypes = 2379,
44+
TypeNotAssignableTypeOfTargetWithExactOptionalPropertyTypes = 2412,
45+
IndexSignatureOnlyPermitsReading = 2542,
4246
NoOverloadExpectsCountOfArguments = 2575,
4347
ThisContextOfTypeNotAssignableToMethodOfThisType = 2684,
4448
PropertyMissingInType1ButRequiredInType2 = 2741,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export type OptionalProperty = {
2+
requiredProp: 'required';
3+
optionalProp?: 'optional';
4+
};
5+
6+
export function setWithOptionalProperty(obj: OptionalProperty): any;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports.default = (foo, bar) => {
2+
return foo + bar;
3+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {expectError} from '../../../..';
2+
import {OptionalProperty, setWithOptionalProperty} from '.';
3+
4+
expectError(() => {
5+
const obj: OptionalProperty = {
6+
requiredProp: 'required',
7+
// ts2375 - setting optional property to undefined
8+
optionalProp: undefined,
9+
};
10+
});
11+
12+
expectError(setWithOptionalProperty({
13+
requiredProp: 'required',
14+
// ts2379 - setting optional property to undefined in a parameter
15+
optionalProp: undefined,
16+
}));
17+
18+
const obj: OptionalProperty = { requiredProp: 'required' };
19+
20+
// ts2412 - setting optional property to undefined by access
21+
expectError(obj.optionalProp = undefined);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "foo",
3+
"tsd": {
4+
"compilerOptions": {
5+
"exactOptionalPropertyTypes": true
6+
}
7+
}
8+
}

source/test/fixtures/expect-error/values/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ export const triggerSuggestion: {
2424
// emit a regular TS2322 error without the "Did you mean..." suggestion.
2525
fooOrBar: 'foo' | 'bar';
2626
};
27+
28+
export type ReadonlyKeys = {
29+
readonly [type: string]: any;
30+
}

source/test/fixtures/expect-error/values/index.test-d.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {expectError} from '../../../..';
2-
import {default as one, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion} from '.';
2+
import {default as one, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion, ReadonlyKeys} from '.';
33

44
expectError<string>(1);
55
expectError<string>('fo');
@@ -37,3 +37,12 @@ expectError(new hasProperty({name: 'foo'}));
3737
expectError(() => {
3838
triggerSuggestion.fooOrBar = 'fooo';
3939
})
40+
41+
expectError(() => {
42+
const foo: ReadonlyKeys = {
43+
bar: 'baz',
44+
};
45+
46+
// ts2542 - trying to modify readonly key
47+
foo.bar = 'bar';
48+
});

source/test/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,12 @@ test('expectError for values (noImplicitAny disabled)', async t => {
309309
verify(t, diagnostics, []);
310310
});
311311

312+
test('expectError for values (exactOptionalPropertyTypes enabled)', async t => {
313+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/enabled-exact-optional-property-types')});
314+
315+
verify(t, diagnostics, []);
316+
});
317+
312318
test('missing import', async t => {
313319
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/missing-import')});
314320

0 commit comments

Comments
 (0)