DefinitelyTyped/types/weak-napi/weak-napi-tests.ts
Hieu Lam 5ad20e47c6 [weak-napi] Add type for weak-napi package (#36869)
* Add type for  package

* Update test for `weak-napi`
2019-07-16 15:45:44 -07:00

27 lines
505 B
TypeScript

import weak = require('weak-napi');
const obj = {a: 123};
const weakReference = weak(obj, () => {
// collected
});
const sameType = weak.get(weakReference);
function foo(a: {a: number}) {}
if (sameType) {
foo(sameType);
}
const anyVar = null as any;
if (weak.isWeakRef(anyVar)) {
const a = anyVar; // WeakRef<any>
}
if (weak.isDead(weakReference)) {
const a = weakReference; // $ExpectType WeakRef<undefined>
const value = weak.get(weakReference); // undefined only possible
}