JSHandle.test-d.ts (1974B)
1 /** 2 * @license 3 * Copyright 2024 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 import type {ElementHandle, JSHandle} from 'puppeteer'; 7 import {expectNotAssignable, expectNotType, expectType} from 'tsd'; 8 9 declare const handle: JSHandle; 10 11 { 12 expectType<unknown>(await handle.evaluate('document')); 13 expectType<number>( 14 await handle.evaluate(() => { 15 return 1; 16 }), 17 ); 18 expectType<HTMLElement>( 19 await handle.evaluate(() => { 20 return document.body; 21 }), 22 ); 23 expectType<string>( 24 await handle.evaluate(() => { 25 return ''; 26 }), 27 ); 28 expectType<string>( 29 await handle.evaluate((value, str) => { 30 expectNotAssignable<never>(value); 31 expectType<string>(str); 32 return ''; 33 }, ''), 34 ); 35 } 36 37 { 38 expectType<JSHandle>(await handle.evaluateHandle('document')); 39 expectType<JSHandle<number>>( 40 await handle.evaluateHandle(() => { 41 return 1; 42 }), 43 ); 44 expectType<JSHandle<string>>( 45 await handle.evaluateHandle(() => { 46 return ''; 47 }), 48 ); 49 expectType<JSHandle<string>>( 50 await handle.evaluateHandle((value, str) => { 51 expectNotAssignable<never>(value); 52 expectType<string>(str); 53 return ''; 54 }, ''), 55 ); 56 expectType<ElementHandle<HTMLElement>>( 57 await handle.evaluateHandle(() => { 58 return document.body; 59 }), 60 ); 61 } 62 63 declare const handle2: JSHandle<{test: number}>; 64 65 { 66 { 67 expectType<JSHandle<number>>(await handle2.getProperty('test')); 68 expectNotType<JSHandle<unknown>>(await handle2.getProperty('test')); 69 } 70 { 71 expectType<JSHandle<unknown>>( 72 await handle2.getProperty('key-doesnt-exist'), 73 ); 74 expectNotType<JSHandle<string>>( 75 await handle2.getProperty('key-doesnt-exist'), 76 ); 77 expectNotType<JSHandle<number>>( 78 await handle2.getProperty('key-doesnt-exist'), 79 ); 80 } 81 } 82 83 { 84 void handle.evaluate((value, other) => { 85 expectType<unknown>(value); 86 expectType<{test: number}>(other); 87 }, handle2); 88 }