gun/types/index.test-d.ts
Bernie Telles 5d5432a9b5
Fix set/get parameters to use Record<string,*>. (#1094)
* Add TypeScript test file & tsd as devDep to run it

This helps those who want to contribute to the TypeScript definitions
by adding:
 * a tool called `tsd,`
 * its corresponding configuration in package.json
 * a teste file index.test-d.ts

Also add a extra line between type definitions to improve readability.

* Fix set/get parameters to use Record<string,*>.

The previous implementation did not allow users to
retrieve items that were inserted using "set," and
it incorrectly used ArrayOf to extract record types.

The previous implementation also prevented users
from ever inserting an array. Although it the database
does not handle arrays elegantly, it allows the user
to insert them. Typing should not prohibit use of
arrays.
2021-07-30 00:20:32 -07:00

121 lines
2.1 KiB
TypeScript

import { expectError } from 'tsd';
import Gun = require('../index');
Gun(['http://server1.com/gun', 'http://server2.com/gun']);
Gun({
s3: {
key: '',
secret: '',
bucket: ''
},
file: 'file/path.json',
uuid() {
return 'xxxxxx';
}
});
interface AppState {
object: {
num: number;
str: string;
/** Comment test */
bool: boolean;
specstr: 'a' | 'b';
obj: {
arr2: Record<string, { foo: number; bar: string }>;
};
};
chatRoom: Record<string, { by: string; message: string }>;
}
const app = new Gun<AppState>();
// Put and get something that was previously put
app.get('object')
.get('bool')
.put(true);
app.get('object')
.get('num')
.put(1);
app.get('object').put({
bool: true
});
// Set and get something that was inserted using `set`.
const appSet = app.get('object')
.get('obj')
.get('arr2');
appSet.set({ foo: 1, bar: '2' });
// getting an auto-generated key may return an undefined value.
appSet.get('stringIdentifier').once(a => a?.foo);
expectError(
app.get('object')
.get('bool')
.put(1));
app.get('object').on(data => {
data.bool;
});
app.get('object').off();
app.get('object').once(data => {
if (data) data.bool;
});
async function name() {
const data = await app.get('object').promise!();
data.put.bool;
}
app.get('chatRoom').time!({ by: 'A', message: 'Hello' });
app.get('chatRoom').time!(msg => {
msg.by;
}, 20);
expectError(
app.get('object').time!({ a: 1 }));
class X {
val: string = 'someString';
b() { }
}
interface BadState {
// Top level primitives
a: 1;
b: {
// Ban functions
c: () => void;
// Ban class
d: typeof X;
// Recursive check for banned types
e: {
f: () => void;
};
};
// Filter, remove functions on prototype.
c: X;
}
const bad = new Gun<BadState>();
expectError(
bad.get('a').put(1));
expectError(bad.get('b')
.get('c')
.put(() => { }));
expectError(bad.get('b')
.get('d')
.put(X));
expectError(
bad.get('b').put({ c: () => { }, d: X, e: { f: () => { } } }));
expectError(
bad.get('c').put(new X()));