Skip to content

Commit

Permalink
Add missing error codes (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Mar 9, 2023
1 parent dfa9bc0 commit 168de40
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1 deletion.
4 changes: 4 additions & 0 deletions source/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const expectErrordiagnosticCodesToIgnore = new Set<DiagnosticCode>([
DiagnosticCode.ThisContextOfTypeNotAssignableToMethodOfThisType,
DiagnosticCode.ValueOfTypeNotCallable,
DiagnosticCode.ExpressionNotCallable,
DiagnosticCode.TypeNotAssignableWithExactOptionalPropertyTypes,
DiagnosticCode.TypeNotAssignableToParameterWithExactOptionalPropertyTypes,
DiagnosticCode.TypeNotAssignableTypeOfTargetWithExactOptionalPropertyTypes,
DiagnosticCode.IndexSignatureOnlyPermitsReading,
DiagnosticCode.OnlyVoidFunctionIsNewCallable,
DiagnosticCode.ExpressionNotConstructable,
DiagnosticCode.NewExpressionTargetLackingConstructSignatureHasAnyType,
Expand Down
4 changes: 4 additions & 0 deletions source/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export enum DiagnosticCode {
ExpressionNotCallable = 2349,
OnlyVoidFunctionIsNewCallable = 2350,
ExpressionNotConstructable = 2351,
TypeNotAssignableWithExactOptionalPropertyTypes = 2375,
TypeNotAssignableToParameterWithExactOptionalPropertyTypes = 2379,
TypeNotAssignableTypeOfTargetWithExactOptionalPropertyTypes = 2412,
IndexSignatureOnlyPermitsReading = 2542,
NoOverloadExpectsCountOfArguments = 2575,
ThisContextOfTypeNotAssignableToMethodOfThisType = 2684,
PropertyMissingInType1ButRequiredInType2 = 2741,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type OptionalProperty = {
requiredProp: 'required';
optionalProp?: 'optional';
};

export function setWithOptionalProperty(obj: OptionalProperty): any;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {expectError} from '../../../..';
import {OptionalProperty, setWithOptionalProperty} from '.';

expectError(() => {
const obj: OptionalProperty = {
requiredProp: 'required',
// ts2375 - setting optional property to undefined
optionalProp: undefined,
};
});

expectError(setWithOptionalProperty({
requiredProp: 'required',
// ts2379 - setting optional property to undefined in a parameter
optionalProp: undefined,
}));

const obj: OptionalProperty = { requiredProp: 'required' };

// ts2412 - setting optional property to undefined by access
expectError(obj.optionalProp = undefined);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "foo",
"tsd": {
"compilerOptions": {
"exactOptionalPropertyTypes": true
}
}
}
4 changes: 4 additions & 0 deletions source/test/fixtures/expect-error/values/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ export const triggerSuggestion: {
// emit a regular TS2322 error without the "Did you mean..." suggestion.
fooOrBar: 'foo' | 'bar';
};

export type ReadonlyKeys = {
readonly [type: string]: any;
}
11 changes: 10 additions & 1 deletion source/test/fixtures/expect-error/values/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectError} from '../../../..';
import {default as one, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion} from '.';
import {default as one, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion, ReadonlyKeys} from '.';

expectError<string>(1);
expectError<string>('fo');
Expand Down Expand Up @@ -37,3 +37,12 @@ expectError(new hasProperty({name: 'foo'}));
expectError(() => {
triggerSuggestion.fooOrBar = 'fooo';
})

expectError(() => {
const foo: ReadonlyKeys = {
bar: 'baz',
};

// ts2542 - trying to modify readonly key
foo.bar = 'bar';
});
6 changes: 6 additions & 0 deletions source/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ test('expectError for values (noImplicitAny disabled)', async t => {
verify(t, diagnostics, []);
});

test('expectError for values (exactOptionalPropertyTypes enabled)', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/enabled-exact-optional-property-types')});

verify(t, diagnostics, []);
});

test('missing import', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/missing-import')});

Expand Down

0 comments on commit 168de40

Please sign in to comment.