From d2245664350e530aae0e62fc87a6256ac3b925a2 Mon Sep 17 00:00:00 2001 From: Ivan Hofer Date: Sat, 12 Feb 2022 09:15:03 +0100 Subject: [PATCH] add types tests --- package.json | 3 +- .../runtime/internal/lifecyle.test-types.ts | 33 +++++++++++++++++++ test/types/tsconfig.json | 28 ++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 test/types/runtime/internal/lifecyle.test-types.ts create mode 100644 test/types/tsconfig.json diff --git a/package.json b/package.json index 2ca79c5fa8..526cdae9e1 100644 --- a/package.json +++ b/package.json @@ -83,8 +83,9 @@ }, "types": "types/runtime/index.d.ts", "scripts": { - "test": "mocha --exit", + "test": "mocha --exit && npm run test:types", "test:unit": "mocha --require sucrase/register --recursive src/**/__test__.ts --exit", + "test:types": "tsc --project test/types/tsconfig.json --noEmit", "quicktest": "mocha", "build": "rollup -c && npm run tsd", "prepare": "npm run build", diff --git a/test/types/runtime/internal/lifecyle.test-types.ts b/test/types/runtime/internal/lifecyle.test-types.ts new file mode 100644 index 0000000000..6190f675a5 --- /dev/null +++ b/test/types/runtime/internal/lifecyle.test-types.ts @@ -0,0 +1,33 @@ +import { createEventDispatcher } from '$runtime/internal/lifecycle'; + +const dispatch = createEventDispatcher<{ + loaded: never + change: string + valid: boolean + optional: number | null +}>(); + +// @ts-expect-error: dispatch invalid event +dispatch('some-event'); + +dispatch('loaded'); +// @ts-expect-error: no detail accepted +dispatch('loaded', 123); + +// @ts-expect-error: detail not provided +dispatch('change'); +dispatch('change', 'string'); +// @ts-expect-error: wrong type of detail +dispatch('change', 123); +// @ts-expect-error: wrong type of detail +dispatch('change', undefined); + +dispatch('valid', true); +// @ts-expect-error: wrong type of detail +dispatch('valid', 'string'); + +dispatch('optional'); +dispatch('optional', 123); +dispatch('optional', null); +// @ts-expect-error: wrong type of optional detail +dispatch('optional', 'string'); diff --git a/test/types/tsconfig.json b/test/types/tsconfig.json new file mode 100644 index 0000000000..027fca0dc1 --- /dev/null +++ b/test/types/tsconfig.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + "moduleResolution": "node", + "module": "ESNext", + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "isolatedModules": true, + "resolveJsonModule": true, + "esModuleInterop": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true, + "baseUrl": "../../", + "allowJs": true, + "checkJs": true, + "paths": { + "$runtime/*": ["src/runtime/*"] + }, + // enable strictest options + "allowUnreachableCode": false, + "alwaysStrict": true, + "noFallthroughCasesInSwitch": true, + "noImplicitAny": true, + "noImplicitReturns": true, + "noImplicitThis": true, + "strict": true, + }, + "include": ["**/*.test-types.ts"] +}