From 168de4046816ec0814bc13090f2d56d9f3273835 Mon Sep 17 00:00:00 2001 From: Tommy Date: Thu, 9 Mar 2023 01:29:39 -0600 Subject: [PATCH] Add missing error codes (#177) --- source/lib/compiler.ts | 4 ++++ source/lib/interfaces.ts | 4 ++++ .../index.d.ts | 6 ++++++ .../index.js | 3 +++ .../index.test-d.ts | 21 +++++++++++++++++++ .../package.json | 8 +++++++ .../fixtures/expect-error/values/index.d.ts | 4 ++++ .../expect-error/values/index.test-d.ts | 11 +++++++++- source/test/test.ts | 6 ++++++ 9 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.d.ts create mode 100644 source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.js create mode 100644 source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.test-d.ts create mode 100644 source/test/fixtures/expect-error/enabled-exact-optional-property-types/package.json diff --git a/source/lib/compiler.ts b/source/lib/compiler.ts index 34ddaaf2..58dc33bc 100644 --- a/source/lib/compiler.ts +++ b/source/lib/compiler.ts @@ -33,6 +33,10 @@ const expectErrordiagnosticCodesToIgnore = new Set([ DiagnosticCode.ThisContextOfTypeNotAssignableToMethodOfThisType, DiagnosticCode.ValueOfTypeNotCallable, DiagnosticCode.ExpressionNotCallable, + DiagnosticCode.TypeNotAssignableWithExactOptionalPropertyTypes, + DiagnosticCode.TypeNotAssignableToParameterWithExactOptionalPropertyTypes, + DiagnosticCode.TypeNotAssignableTypeOfTargetWithExactOptionalPropertyTypes, + DiagnosticCode.IndexSignatureOnlyPermitsReading, DiagnosticCode.OnlyVoidFunctionIsNewCallable, DiagnosticCode.ExpressionNotConstructable, DiagnosticCode.NewExpressionTargetLackingConstructSignatureHasAnyType, diff --git a/source/lib/interfaces.ts b/source/lib/interfaces.ts index 363a5fa3..261148e5 100644 --- a/source/lib/interfaces.ts +++ b/source/lib/interfaces.ts @@ -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, diff --git a/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.d.ts b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.d.ts new file mode 100644 index 00000000..0872acc1 --- /dev/null +++ b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.d.ts @@ -0,0 +1,6 @@ +export type OptionalProperty = { + requiredProp: 'required'; + optionalProp?: 'optional'; +}; + +export function setWithOptionalProperty(obj: OptionalProperty): any; diff --git a/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.js b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.js new file mode 100644 index 00000000..f17717f5 --- /dev/null +++ b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.js @@ -0,0 +1,3 @@ +module.exports.default = (foo, bar) => { + return foo + bar; +}; diff --git a/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.test-d.ts b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.test-d.ts new file mode 100644 index 00000000..ef8743f6 --- /dev/null +++ b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/index.test-d.ts @@ -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); diff --git a/source/test/fixtures/expect-error/enabled-exact-optional-property-types/package.json b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/package.json new file mode 100644 index 00000000..52e19d2e --- /dev/null +++ b/source/test/fixtures/expect-error/enabled-exact-optional-property-types/package.json @@ -0,0 +1,8 @@ +{ + "name": "foo", + "tsd": { + "compilerOptions": { + "exactOptionalPropertyTypes": true + } + } +} diff --git a/source/test/fixtures/expect-error/values/index.d.ts b/source/test/fixtures/expect-error/values/index.d.ts index cd6ddd3a..783a63fd 100644 --- a/source/test/fixtures/expect-error/values/index.d.ts +++ b/source/test/fixtures/expect-error/values/index.d.ts @@ -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; +} 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 4a2ceeaf..70bc65f7 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, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion} from '.'; +import {default as one, atLeastOne, foo, getFoo, HasKey, hasProperty, MyClass, Options, triggerSuggestion, ReadonlyKeys} from '.'; expectError(1); expectError('fo'); @@ -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'; +}); diff --git a/source/test/test.ts b/source/test/test.ts index b29f9063..8f51278f 100644 --- a/source/test/test.ts +++ b/source/test/test.ts @@ -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')});