From 2f585fe0b6a887da692e47af7f5c761b58bf2e45 Mon Sep 17 00:00:00 2001 From: Nick McCurdy Date: Tue, 10 Nov 2020 22:28:34 -0500 Subject: [PATCH] fix(type): only return Promise with delay (#491) Co-authored-by: Gerrit Alex Co-authored-by: Nick McCurdy Co-authored-by: Kent C. Dodds --- package.json | 8 +++++--- typings/index.d.ts | 6 +++--- typings/test.ts | 8 ++++++++ typings/tsconfig.json | 7 +++++++ 4 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 typings/test.ts create mode 100644 typings/tsconfig.json diff --git a/package.json b/package.json index e881e98f..62edb9bc 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "homepage": "https://github.com/testing-library/user-event#readme", "files": [ "dist", - "typings" + "typings/index.d.ts" ], "scripts": { "build": "kcd-scripts build", @@ -35,7 +35,8 @@ "test": "kcd-scripts test", "test:debug": "kcd-scripts --inspect-brk test --runInBand", "test:update": "npm test -- --updateSnapshot --coverage", - "validate": "kcd-scripts validate" + "validate": "kcd-scripts validate", + "typecheck": "tsc --project typings" }, "dependencies": { "@babel/runtime": "^7.10.2" @@ -46,7 +47,8 @@ "@types/estree": "0.0.45", "is-ci": "^2.0.0", "jest-serializer-ansi": "^1.0.3", - "kcd-scripts": "^6.2.3" + "kcd-scripts": "^6.2.3", + "typescript": "^4.0.5" }, "peerDependencies": { "@testing-library/dom": ">=7.21.4" diff --git a/typings/index.d.ts b/typings/index.d.ts index be62ed3a..74158a08 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -53,11 +53,11 @@ declare const userEvent: { files: FilesArgument, init?: UploadInitArgument, ) => void - type: ( + type: ( element: TargetElement, text: string, - userOpts?: ITypeOpts, - ) => Promise + userOpts?: T, + ) => T extends {delay: number} ? Promise : void tab: (userOpts?: ITabUserOptions) => void paste: ( element: TargetElement, diff --git a/typings/test.ts b/typings/test.ts new file mode 100644 index 00000000..51777473 --- /dev/null +++ b/typings/test.ts @@ -0,0 +1,8 @@ +import userEvent, {TargetElement} from '.' + +declare const element: TargetElement +type NotVoid = string | number | boolean | object | null + +userEvent.type(element, 'foo', {delay: 1}) as Promise +// @ts-expect-error No delay returns void +userEvent.type(element, 'foo') as NotVoid diff --git a/typings/tsconfig.json b/typings/tsconfig.json new file mode 100644 index 00000000..d45d4e44 --- /dev/null +++ b/typings/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "lib": ["dom"], + "noEmit": true, + "strict": true + } +}