Skip to content

Commit a603fb0

Browse files
committed
Add Typescript annotations
1 parent 61d3a19 commit a603fb0

File tree

12 files changed

+486
-0
lines changed

12 files changed

+486
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { PropertyName } from '@stdlib/types/object';
24+
25+
/**
26+
* Getter function.
27+
*
28+
* @returns property value
29+
*/
30+
type Getter = () => any;
31+
32+
/**
33+
* Defines a read-only accessor.
34+
*
35+
* ## Notes
36+
*
37+
* - Read-only accessors are enumerable and non-configurable.
38+
*
39+
* @param obj - object on which to define the property
40+
* @param prop - property name
41+
* @param getter - accessor
42+
*
43+
* @example
44+
* function getter() {
45+
* return 'bar';
46+
* }
47+
*
48+
* var obj = {};
49+
*
50+
* setReadOnlyAccessor( obj, 'foo', getter );
51+
*
52+
* try {
53+
* obj.foo = 'boop';
54+
* } catch ( err ) {
55+
* console.error( err.message );
56+
* }
57+
*/
58+
declare function setReadOnlyAccessor( obj: any, prop: PropertyName, getter: Getter ): void; // tslint:disable-line: max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = setReadOnlyAccessor;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
import setReadOnlyAccessor = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns `undefined`...
25+
{
26+
setReadOnlyAccessor( {}, 'foo', (): string => 'bar' ); // $ExpectType void
27+
}
28+
29+
// The compiler throws an error if the function is provided a second argument which is not a valid property name...
30+
{
31+
setReadOnlyAccessor( {}, true, (): string => 'bar' ); // $ExpectError
32+
setReadOnlyAccessor( {}, false, (): string => 'bar' ); // $ExpectError
33+
setReadOnlyAccessor( {}, null, (): string => 'bar' ); // $ExpectError
34+
setReadOnlyAccessor( {}, undefined, (): string => 'bar' ); // $ExpectError
35+
setReadOnlyAccessor( {}, [], (): string => 'bar' ); // $ExpectError
36+
setReadOnlyAccessor( {}, {}, (): string => 'bar' ); // $ExpectError
37+
setReadOnlyAccessor( {}, ( x: number ): number => x, (): string => 'bar' ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided a third argument which is not a valid getter...
41+
{
42+
setReadOnlyAccessor( {}, 'foo', '5' ); // $ExpectError
43+
setReadOnlyAccessor( {}, 'foo', 5 ); // $ExpectError
44+
setReadOnlyAccessor( {}, 'foo', true ); // $ExpectError
45+
setReadOnlyAccessor( {}, 'foo', false ); // $ExpectError
46+
setReadOnlyAccessor( {}, 'foo', null ); // $ExpectError
47+
setReadOnlyAccessor( {}, 'foo', undefined ); // $ExpectError
48+
setReadOnlyAccessor( {}, 'foo', [] ); // $ExpectError
49+
setReadOnlyAccessor( {}, 'foo', {} ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided insufficient arguments...
53+
{
54+
setReadOnlyAccessor(); // $ExpectError
55+
setReadOnlyAccessor( {} ); // $ExpectError
56+
setReadOnlyAccessor( {}, 'foo' ); // $ExpectError
57+
}

lib/node_modules/@stdlib/utils/define-read-only-accessor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { PropertyName } from '@stdlib/types/object';
24+
25+
/**
26+
* Defines a read-only property.
27+
*
28+
* ## Notes
29+
*
30+
* - Read-only properties are enumerable and non-configurable.
31+
*
32+
* @param obj - object on which to define the property
33+
* @param prop - property name
34+
* @param value - value to set
35+
*
36+
* @example
37+
* var obj = {};
38+
*
39+
* setReadOnly( obj, 'foo', 'bar' );
40+
*
41+
* try {
42+
* obj.foo = 'boop';
43+
* } catch ( err ) {
44+
* console.error( err.message );
45+
* }
46+
*/
47+
declare function setReadOnly( obj: any, prop: PropertyName, value: any ): void; // tslint:disable-line: max-line-length
48+
49+
50+
// EXPORTS //
51+
52+
export = setReadOnly;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
import setReadOnly = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns `undefined`...
25+
{
26+
setReadOnly( {}, 'foo', 'bar' ); // $ExpectType void
27+
}
28+
29+
// The compiler throws an error if the function is provided a second argument which is not a valid property name...
30+
{
31+
setReadOnly( {}, true, 'bar' ); // $ExpectError
32+
setReadOnly( {}, false, 'bar' ); // $ExpectError
33+
setReadOnly( {}, null, 'bar' ); // $ExpectError
34+
setReadOnly( {}, undefined, 'bar' ); // $ExpectError
35+
setReadOnly( {}, [], 'bar' ); // $ExpectError
36+
setReadOnly( {}, {}, 'bar' ); // $ExpectError
37+
setReadOnly( {}, ( x: number ): number => x, 'bar' ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided insufficient arguments...
41+
{
42+
setReadOnly(); // $ExpectError
43+
setReadOnly( {} ); // $ExpectError
44+
setReadOnly( {}, 'foo' ); // $ExpectError
45+
}

lib/node_modules/@stdlib/utils/define-read-only-property/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
/// <reference types="@stdlib/types"/>
22+
23+
import { PropertyName } from '@stdlib/types/object';
24+
25+
/**
26+
* Getter function.
27+
*
28+
* @returns property value
29+
*/
30+
type Getter = () => any;
31+
32+
/**
33+
* Setter function.
34+
*
35+
* @param value - property value
36+
*/
37+
type Setter = ( x: any ) => void;
38+
39+
/**
40+
* Defines a read-write accessor.
41+
*
42+
* ## Notes
43+
*
44+
* - Read-write accessors are enumerable and non-configurable.
45+
*
46+
* @param obj - object on which to define the property
47+
* @param prop - property name
48+
* @param getter - get accessor
49+
* @param setter - set accessor
50+
*
51+
* @example
52+
* function getter() {
53+
* return name + ' foo';
54+
* }
55+
*
56+
* function setter( v ) {
57+
* name = v;
58+
* }
59+
*
60+
* var name = 'bar';
61+
* var obj = {};
62+
*
63+
* setReadWriteAccessor( obj, 'foo', getter, setter );
64+
*
65+
* var v = obj.foo;
66+
* // returns 'bar foo'
67+
*
68+
* obj.foo = 'beep';
69+
*
70+
* v = obj.foo;
71+
* // returns 'beep foo'
72+
*/
73+
declare function setReadWriteAccessor( obj: any, prop: PropertyName, getter: Getter, setter: Setter ): void; // tslint:disable-line: max-line-length
74+
75+
76+
// EXPORTS //
77+
78+
export = setReadWriteAccessor;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
import setReadWriteAccessor = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns `undefined`...
25+
{
26+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectType void
27+
}
28+
29+
// The compiler throws an error if the function is provided a second argument which is not a valid property name...
30+
{
31+
setReadWriteAccessor( {}, true, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
32+
setReadWriteAccessor( {}, false, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
33+
setReadWriteAccessor( {}, null, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
34+
setReadWriteAccessor( {}, undefined, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
35+
setReadWriteAccessor( {}, [], (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
36+
setReadWriteAccessor( {}, {}, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
37+
setReadWriteAccessor( {}, ( x: number ): number => x, (): string => 'beep', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
38+
}
39+
40+
// The compiler throws an error if the function is provided a third argument which is not a valid getter...
41+
{
42+
setReadWriteAccessor( {}, 'foo', '5', ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
43+
setReadWriteAccessor( {}, 'foo', 5, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
44+
setReadWriteAccessor( {}, 'foo', true, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
45+
setReadWriteAccessor( {}, 'foo', false, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
46+
setReadWriteAccessor( {}, 'foo', null, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
47+
setReadWriteAccessor( {}, 'foo', undefined, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
48+
setReadWriteAccessor( {}, 'foo', [], ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
49+
setReadWriteAccessor( {}, 'foo', {}, ( x: any ): void => { throw new Error( String( x ) ); } ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a fourth argument which is not a valid setter...
53+
{
54+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', '5' ); // $ExpectError
55+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', 5 ); // $ExpectError
56+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', true ); // $ExpectError
57+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', false ); // $ExpectError
58+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', null ); // $ExpectError
59+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', undefined ); // $ExpectError
60+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', [] ); // $ExpectError
61+
setReadWriteAccessor( {}, 'foo', (): string => 'beep', {} ); // $ExpectError
62+
}
63+
64+
// The compiler throws an error if the function is provided insufficient arguments...
65+
{
66+
setReadWriteAccessor(); // $ExpectError
67+
setReadWriteAccessor( {} ); // $ExpectError
68+
setReadWriteAccessor( {}, 'foo' ); // $ExpectError
69+
setReadWriteAccessor( {}, 'foo', (): string => 'beep' ); // $ExpectError
70+
}

lib/node_modules/@stdlib/utils/define-read-write-accessor/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"lib": "./lib",
2222
"test": "./test"
2323
},
24+
"types": "./docs/types",
2425
"scripts": {},
2526
"homepage": "https://github.com/stdlib-js/stdlib",
2627
"repository": {

0 commit comments

Comments
 (0)