Skip to content
This repository was archived by the owner on Nov 18, 2021. It is now read-only.

Commit e14111e

Browse files
committed
style: 完善类型注释
1 parent e2f00e4 commit e14111e

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

src/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
export interface FnObj {
2-
[prop: string]: (...args: any[]) => any
1+
interface A {
2+
[prop: string]: any
33
}
44

5-
export default function<T extends FnObj>(
5+
function chromeCall<T extends A>(
6+
object: T,
7+
methodName: keyof T,
8+
args: any | any[],
9+
returnArray: true
10+
): Promise<any[]>
11+
function chromeCall<T extends A>(
12+
object: T,
13+
methodName: keyof T,
14+
args: any | any[],
15+
returnArray?: boolean
16+
): Promise<any>
17+
function chromeCall<T extends A>(
618
object: T,
719
methodName: keyof T,
820
args: any | any[] = [],
921
returnArray?: boolean
10-
): Promise<any> {
22+
) {
1123
return new Promise((resolve, reject) => {
1224
if (!Array.isArray(args)) {
1325
args = [args]
@@ -25,6 +37,8 @@ export default function<T extends FnObj>(
2537
resolve(results[0])
2638
}
2739
})
28-
object[methodName].apply(object, args)
40+
;(object[methodName] as Function).apply(object, args)
2941
})
3042
}
43+
44+
export default chromeCall

test/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chromeCall, { FnObj } from '../src/index'
1+
import chromeCall from '../src/index'
22
import { polyfill } from 'es6-promise'
33

44
polyfill()
@@ -8,7 +8,7 @@ window.chrome = {
88
runtime: {}
99
}
1010

11-
interface FakeObj extends FnObj {
11+
interface FakeObj {
1212
method: (key: string, cb: (...args: any[]) => any) => void
1313
}
1414

@@ -30,7 +30,7 @@ describe('chromeCall', () => {
3030

3131
it('若正常执行则 resolve promise', done => {
3232
chromeCall(fakeObj, 'method', ['a']).then(
33-
(value: any) => {
33+
value => {
3434
expect(value).toBe('a')
3535
done()
3636
},
@@ -57,7 +57,7 @@ describe('chromeCall', () => {
5757

5858
it('最后一个参数是 true,则返回一个数组', done => {
5959
chromeCall(fakeObj, 'method', ['a'], true).then(
60-
(value: any) => {
60+
value => {
6161
expect(Array.isArray(value)).toBe(true)
6262
expect(value).toEqual(['a', 'b'])
6363
done()

0 commit comments

Comments
 (0)