From 27c93af75f50b86c4064c958ede68e48b9b015ad Mon Sep 17 00:00:00 2001 From: Sakumatti Luukkonen Date: Tue, 25 May 2021 20:34:11 +0300 Subject: [PATCH] Detect "Type X has no properties in common with type Y" in `expectError` (TS2559) (#97) --- source/lib/compiler.ts | 3 ++- source/lib/interfaces.ts | 1 + source/test/fixtures/expect-error/values/index.d.ts | 4 ++++ source/test/fixtures/expect-error/values/index.test-d.ts | 4 +++- 4 files changed, 10 insertions(+), 2 deletions(-) diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index e456e9ef..29d0c8e9 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -25,7 +25,8 @@ const expectErrordiagnosticCodesToIgnore = new Set([ DiagnosticCode.GenericTypeRequiresTypeArguments, DiagnosticCode.ExpectedArgumentsButGotOther, DiagnosticCode.NoOverloadMatches, - DiagnosticCode.PropertyMissingInType1ButRequiredInType2 + DiagnosticCode.PropertyMissingInType1ButRequiredInType2, + DiagnosticCode.TypeHasNoPropertiesInCommonWith ]); /** diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index ef957d92..2fbc11bc 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -29,6 +29,7 @@ export enum DiagnosticCode { ArgumentTypeIsNotAssignableToParameterType = 2345, CannotAssignToReadOnlyProperty = 2540, ExpectedArgumentsButGotOther = 2554, + TypeHasNoPropertiesInCommonWith = 2559, NoOverloadMatches = 2769, PropertyMissingInType1ButRequiredInType2 = 2741, } diff --git a/source/test/fixtures/expect-error/values/index.d.ts b/source/test/fixtures/expect-error/values/index.d.ts index 6ad42d78..3b8e73e2 100644 --- a/source/test/fixtures/expect-error/values/index.d.ts +++ b/source/test/fixtures/expect-error/values/index.d.ts @@ -9,4 +9,8 @@ export const foo: {readonly bar: string}; export function hasProperty(property: {name: string}): boolean; +export type HasKey = {[P in K]?: V}; + +export function getFoo>(obj: T): T['foo']; + export interface Options {} diff --git a/source/test/fixtures/expect-error/values/index.test-d.ts b/source/test/fixtures/expect-error/values/index.test-d.ts index 01d84f08..8978ae3e 100644 --- a/source/test/fixtures/expect-error/values/index.test-d.ts +++ b/source/test/fixtures/expect-error/values/index.test-d.ts @@ -1,5 +1,5 @@ import {expectError} from '../../../..'; -import {default as one, foo, hasProperty, Options} from '.'; +import {default as one, foo, getFoo, HasKey, hasProperty, Options} from '.'; expectError(1); expectError('fo'); @@ -20,3 +20,5 @@ expectError(hasProperty({name: 1})); expectError(one(1)); expectError(one(1, 2, 3)); expectError({} as Options); + +expectError(getFoo({bar: 1} as HasKey<'bar'>));