Skip to content

Commit 5681fe1

Browse files
authored
Merge pull request #5 from jemmyphan/master
fix array undefined error
2 parents 17506a4 + 0e3bbf6 commit 5681fe1

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

__tests__/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ describe("Option", () => {
5454
expect(optionalObj.i(100).type).toBe(OptionType.None);
5555
});
5656
it("can be called after k function", () => {
57-
const obj = {
58-
a: [0, 1, 2]
57+
type ArrObj = {
58+
a: number[]
59+
b?: number[]
60+
}
61+
const obj: ArrObj = {
62+
a: [0, 1, 2],
5963
};
6064
const optionalObj = new Option(obj);
6165
expect(
@@ -70,6 +74,12 @@ describe("Option", () => {
7074
.i(1)
7175
.get()
7276
).toBe(1);
77+
expect(
78+
optionalObj
79+
.k("b")
80+
.i(0)
81+
.get()
82+
).toBe(undefined)
7383
});
7484
});
7585

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class Option<T> {
5151
}
5252

5353
Option.prototype.i = function<U>(index: number): Option<U> {
54-
if (this.value[index] === undefined) {
54+
if (this.value === undefined || this.value[index] === undefined) {
5555
return new Option(undefined as any);
5656
}
5757
return new Option(this.value[index]);

0 commit comments

Comments
 (0)