Skip to content

Commit abf7082

Browse files
Allow expectError assertions to detect this type mismatches on class methods (#105)
1 parent 27c93af commit abf7082

File tree

7 files changed

+31
-1
lines changed

7 files changed

+31
-1
lines changed

source/lib/compiler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ const expectErrordiagnosticCodesToIgnore = new Set<DiagnosticCode>([
2626
DiagnosticCode.ExpectedArgumentsButGotOther,
2727
DiagnosticCode.NoOverloadMatches,
2828
DiagnosticCode.PropertyMissingInType1ButRequiredInType2,
29-
DiagnosticCode.TypeHasNoPropertiesInCommonWith
29+
DiagnosticCode.TypeHasNoPropertiesInCommonWith,
30+
DiagnosticCode.ThisContextOfTypeNotAssignableToMethodOfThisType
3031
]);
3132

3233
/**

source/lib/interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export enum DiagnosticCode {
3232
TypeHasNoPropertiesInCommonWith = 2559,
3333
NoOverloadMatches = 2769,
3434
PropertyMissingInType1ButRequiredInType2 = 2741,
35+
ThisContextOfTypeNotAssignableToMethodOfThisType = 2684,
3536
}
3637

3738
export interface Diagnostic {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare class Foo<T> {
2+
public bar(this: Foo<void>): void;
3+
public bar(value: T): void;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Foo {
2+
bar(value) {
3+
if (value) {
4+
this.val = value;
5+
}
6+
}
7+
}
8+
9+
module.exports.Foo = Foo;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {expectError} from '../../../..';
2+
import {Foo} from '.';
3+
4+
const numberFoo = new Foo<number>();
5+
6+
expectError(numberFoo.bar());
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "foo"
3+
}

source/test/test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ test('support setting a custom test directory', async t => {
183183
]);
184184
});
185185

186+
test('expectError for classes', async t => {
187+
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/classes')});
188+
189+
verify(t, diagnostics, []);
190+
});
191+
186192
test('expectError for functions', async t => {
187193
const diagnostics = await tsd({cwd: path.join(__dirname, 'fixtures/expect-error/functions')});
188194

0 commit comments

Comments
 (0)