Skip to content

Commit 5666ca4

Browse files
authored
fix(type): add undefined type to attributes() for missing key (#1398)
1 parent 794b192 commit 5666ca4

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

src/baseWrapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ export default abstract class BaseWrapper<ElementType extends Node>
197197
}
198198

199199
attributes(): { [key: string]: string }
200-
attributes(key: string): string
201-
attributes(key?: string): { [key: string]: string } | string {
200+
attributes(key: string): string | undefined
201+
attributes(key?: string): { [key: string]: string } | string | undefined {
202202
const attributeMap: Record<string, string> = {}
203203
if (isElement(this.element)) {
204204
const attributes = Array.from(this.element.attributes)

src/interfaces/wrapperLike.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export default interface WrapperLike {
9999
classes(className?: string): string[] | boolean
100100

101101
attributes(): { [key: string]: string }
102-
attributes(key: string): string
103-
attributes(key?: string): { [key: string]: string } | string
102+
attributes(key: string): string | undefined
103+
attributes(key?: string): { [key: string]: string } | string | undefined
104104

105105
text(): string
106106
exists(): boolean

test-dts/wrapper.d-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ expectType<Element | undefined>(byClassArray[0].element)
6464
// emitted
6565
// event name
6666
let incrementEvent = wrapper.emitted<{ count: number }>('increment')
67-
expectType<{ count: number }>(incrementEvent[0])
67+
expectType<{ count: number }[] | undefined>(incrementEvent)
68+
expectType<{ count: number }>(incrementEvent![0])
6869

6970
// without event name
7071
let allEvents = wrapper.emitted()
@@ -98,9 +99,9 @@ expectType<Element>(byClass.element)
9899

99100
// attributes
100101
expectType<{ [key: string]: string }>(wrapper.attributes())
101-
expectType<string>(wrapper.attributes('key'))
102+
expectType<string | undefined>(wrapper.attributes('key'))
102103
expectType<{ [key: string]: string }>(domWrapper.attributes())
103-
expectType<string>(domWrapper.attributes('key'))
104+
expectType<string | undefined>(domWrapper.attributes('key'))
104105

105106
// classes
106107
expectType<Array<string>>(wrapper.classes())

0 commit comments

Comments
 (0)