Skip to content

Commit 03837e2

Browse files
committed
Add declaration file containing ambient modules
1 parent 6772656 commit 03837e2

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Ambient module containing iterator definitions.
23+
*/
24+
declare module '@stdlib/types/iterator' {
25+
interface Iterator {
26+
/**
27+
* Returns an iterator protocol-compliant object containing the next iterated value (if one exists) and a boolean flag indicating whether the iterator is finished.
28+
*
29+
* @returns iterator protocol-compliant object
30+
*/
31+
next(): IteratorResult;
32+
33+
/**
34+
* Finishes an iterator.
35+
*
36+
* @param value - value to return
37+
* @returns iterator protocol-compliant object
38+
*/
39+
return?( value?: any ): IteratorResult;
40+
}
41+
42+
interface IterableIterator extends Iterator {
43+
/**
44+
* Returns a new iterable iterator.
45+
*
46+
* @returns iterable iterator
47+
*/
48+
[Symbol.iterator](): IterableIterator;
49+
}
50+
51+
interface IteratorResult {
52+
/**
53+
* Iterated value (if one exists).
54+
*/
55+
value?: any;
56+
57+
/**
58+
* Boolean flag indicating whether an iterator is finished.
59+
*/
60+
done: boolean;
61+
}
62+
}
63+
64+
declare module '@stdlib/types/TODO' {
65+
interface TODO {
66+
/**
67+
* TODO: remove me once more than one module defined in this file.
68+
*/
69+
todo: boolean;
70+
}
71+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2019 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/// <reference types="@stdlib/types"/>
20+
21+
import * as stdlib from '@stdlib/types/iterator';
22+
23+
/**
24+
* Returns an iterator protocol-compliant object.
25+
*
26+
* @returns iterator protocol-compliant object
27+
*/
28+
function createIterator1(): stdlib.Iterator {
29+
return {
30+
'next': next
31+
};
32+
33+
/**
34+
* Implements the iterator protocol `next` method.
35+
*
36+
* @returns iterator protocol-compliant object
37+
*/
38+
function next(): stdlib.IteratorResult {
39+
return {
40+
'value': true,
41+
'done': false
42+
};
43+
}
44+
}
45+
46+
/**
47+
* Returns an iterator protocol-compliant object.
48+
*
49+
* @returns iterator protocol-compliant object
50+
*/
51+
function createIterator2(): stdlib.Iterator {
52+
return {
53+
'next': next
54+
};
55+
56+
/**
57+
* Implements the iterator protocol `next` method.
58+
*
59+
* @returns iterator protocol-compliant object
60+
*/
61+
function next(): stdlib.IteratorResult {
62+
return {
63+
'done': true
64+
};
65+
}
66+
}
67+
68+
/**
69+
* Returns an iterable iterator protocol-compliant object.
70+
*
71+
* @returns iterable iterator protocol-compliant object
72+
*/
73+
function createIterableIterator(): stdlib.IterableIterator {
74+
return {
75+
'next': next,
76+
[ Symbol.iterator ]: factory
77+
};
78+
79+
/**
80+
* Implements the iterator protocol `next` method.
81+
*
82+
* @returns iterator protocol-compliant object
83+
*/
84+
function next(): stdlib.IteratorResult {
85+
return {
86+
'done': true
87+
};
88+
}
89+
90+
/**
91+
* Returns an iterable iterator protocol-compliant object.
92+
*
93+
* @returns iterable iterator protocol-compliant object
94+
*/
95+
function factory(): stdlib.IterableIterator {
96+
return createIterableIterator();
97+
}
98+
}
99+
100+
101+
// TESTS //
102+
103+
// The compiler should not throw an error when a function returns an iterators and iterables...
104+
{
105+
createIterator1();
106+
createIterator2();
107+
createIterableIterator();
108+
}

0 commit comments

Comments
 (0)