Skip to content

Commit

Permalink
Allow expectError assertions to detect this type mismatches on cl…
Browse files Browse the repository at this point in the history
…ass methods (#105)
  • Loading branch information
BendingBender committed May 25, 2021
1 parent 27c93af commit abf7082
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 1 deletion.
3 changes: 2 additions & 1 deletion source/lib/compiler.ts
Expand Up @@ -26,7 +26,8 @@ const expectErrordiagnosticCodesToIgnore = new Set<DiagnosticCode>([
DiagnosticCode.ExpectedArgumentsButGotOther,
DiagnosticCode.NoOverloadMatches,
DiagnosticCode.PropertyMissingInType1ButRequiredInType2,
DiagnosticCode.TypeHasNoPropertiesInCommonWith
DiagnosticCode.TypeHasNoPropertiesInCommonWith,
DiagnosticCode.ThisContextOfTypeNotAssignableToMethodOfThisType
]);

/**
Expand Down
1 change: 1 addition & 0 deletions source/lib/interfaces.ts
Expand Up @@ -32,6 +32,7 @@ export enum DiagnosticCode {
TypeHasNoPropertiesInCommonWith = 2559,
NoOverloadMatches = 2769,
PropertyMissingInType1ButRequiredInType2 = 2741,
ThisContextOfTypeNotAssignableToMethodOfThisType = 2684,
}

export interface Diagnostic {
Expand Down
4 changes: 4 additions & 0 deletions source/test/fixtures/expect-error/classes/index.d.ts
@@ -0,0 +1,4 @@
export declare class Foo<T> {
public bar(this: Foo<void>): void;
public bar(value: T): void;
}
9 changes: 9 additions & 0 deletions source/test/fixtures/expect-error/classes/index.js
@@ -0,0 +1,9 @@
class Foo {
bar(value) {
if (value) {
this.val = value;
}
}
}

module.exports.Foo = Foo;
6 changes: 6 additions & 0 deletions source/test/fixtures/expect-error/classes/index.test-d.ts
@@ -0,0 +1,6 @@
import {expectError} from '../../../..';
import {Foo} from '.';

const numberFoo = new Foo<number>();

expectError(numberFoo.bar());
3 changes: 3 additions & 0 deletions source/test/fixtures/expect-error/classes/package.json
@@ -0,0 +1,3 @@
{
"name": "foo"
}
6 changes: 6 additions & 0 deletions source/test/test.ts
Expand Up @@ -183,6 +183,12 @@ test('support setting a custom test directory', async t => {
]);
});

test('expectError for classes', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/classes')});

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

test('expectError for functions', async t => {
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/functions')});

Expand Down

0 comments on commit abf7082

Please sign in to comment.