Skip to content

Commit e6586be

Browse files
authored
Merge pull request #122 from cexbrayat/chore/wrapper-overloads
chore: wrapper overloads signatures
2 parents 556f2d6 + 60d048a commit e6586be

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

src/dom-wrapper.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@ export class DOMWrapper<ElementType extends Element> {
1010
this.element = element
1111
}
1212

13-
classes(className?: string) {
13+
classes(): string[]
14+
classes(className: string): boolean
15+
classes(className?: string): string[] | boolean {
1416
const classes = this.element.classList
1517

1618
if (className) return classes.contains(className)
1719

1820
return Array.from(classes)
1921
}
2022

21-
attributes(key?: string) {
22-
const attributes = this.element.attributes
23-
const attributeMap = {}
24-
for (let i = 0; i < attributes.length; i++) {
25-
const att = attributes.item(i)
26-
attributeMap[att.localName] = att.value
23+
attributes(): { [key: string]: string }
24+
attributes(key: string): string
25+
attributes(key?: string): { [key: string]: string } | string {
26+
const attributes = Array.from(this.element.attributes)
27+
const attributeMap: Record<string, string> = {}
28+
for (const attribute of attributes) {
29+
attributeMap[attribute.localName] = attribute.value
2730
}
2831

2932
return key ? attributeMap[key] : attributeMap

src/vue-wrapper.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,22 @@ export class VueWrapper<T extends ComponentPublicInstance> {
4545
return this.componentVM
4646
}
4747

48-
props(selector?: string) {
48+
props(): { [key: string]: any }
49+
props(selector: string): any
50+
props(selector?: string): { [key: string]: any } | any {
4951
const props = this.componentVM.$props as { [key: string]: any }
5052
return selector ? props[selector] : props
5153
}
5254

53-
classes(className?: string) {
55+
classes(): string[]
56+
classes(className: string): boolean
57+
classes(className?: string): string[] | boolean {
5458
return new DOMWrapper(this.element).classes(className)
5559
}
5660

57-
attributes(key?: string) {
61+
attributes(): { [key: string]: string }
62+
attributes(key: string): string
63+
attributes(key?: string): { [key: string]: string } | string {
5864
return new DOMWrapper(this.element).attributes(key)
5965
}
6066

test-dts/wrapper.d-test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,19 @@ expectType<SVGLineElement>(line.element)
8686
// string selector
8787
byClass = domWrapper.get('.todo')
8888
expectType<Element>(byClass.element)
89+
90+
// attributes
91+
expectType<{ [key: string]: string }>(wrapper.attributes())
92+
expectType<string>(wrapper.attributes('key'))
93+
expectType<{ [key: string]: string }>(domWrapper.attributes())
94+
expectType<string>(domWrapper.attributes('key'))
95+
96+
// classes
97+
expectType<Array<string>>(wrapper.classes())
98+
expectType<boolean>(wrapper.classes('class'))
99+
expectType<Array<string>>(domWrapper.classes())
100+
expectType<boolean>(domWrapper.classes('class'))
101+
102+
// props
103+
expectType<{ [key: string]: any }>(wrapper.props())
104+
expectType<any>(wrapper.props('prop'))

0 commit comments

Comments
 (0)