Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(types): ScalarKey #2273

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions packages/patterns/src/keys/checkKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { checkBagEntries, makeBagOfEntries } from './copyBag.js';
const { ownKeys } = Reflect;

/**
* @import {Passable} from '@endo/pass-style'
* @import {Passable, Primitive} from '@endo/pass-style'
* @import {Checker} from '@endo/marshal'
* @import {CopyBag, CopyMap, CopySet, Key} from '../types.js'
* @import {CopyBag, CopyMap, CopySet, Key, ScalarKey} from '../types.js'
*/

// ////////////////// Primitive and Scalar keys ////////////////////////////////
Expand All @@ -48,22 +48,22 @@ const checkPrimitiveKey = (val, check) => {

/**
* @param {any} val
* @returns {boolean}
* @returns {val is Primitive}
*/
export const isPrimitiveKey = val => checkPrimitiveKey(val, identChecker);
harden(isPrimitiveKey);

/**
* @param {Passable} val
* @returns {void}
* @returns {asserts val is Primitive}
*/
export const assertPrimitiveKey = val => {
checkPrimitiveKey(val, assertChecker);
};
harden(assertPrimitiveKey);

/**
* @param {Passable} val
* @param {any} val
* @param {Checker} check
* @returns {boolean}
*/
Expand All @@ -80,14 +80,14 @@ export const checkScalarKey = (val, check) => {

/**
* @param {any} val
* @returns {boolean}
* @returns {val is ScalarKey}
*/
export const isScalarKey = val => checkScalarKey(val, identChecker);
harden(isScalarKey);

/**
* @param {Passable} val
* @returns {void}
* @returns {asserts val is ScalarKey}
*/
export const assertScalarKey = val => {
checkScalarKey(val, assertChecker);
Expand Down
6 changes: 5 additions & 1 deletion packages/patterns/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export {};

// NB: as of TS 5.5 nightly, TS thinks RankCover and Checker "is declared but never read" but they are
/**
* @import {Checker, CopyArray, CopyRecord, CopyTagged, Passable, PassStyle, RemotableObject} from '@endo/pass-style';
* @import {Checker, CopyArray, CopyRecord, CopyTagged, Passable, PassStyle, Primitive, RemotableObject} from '@endo/pass-style';
* @import {RankCompare, RankCover} from '@endo/marshal';
*/

Expand Down Expand Up @@ -34,6 +34,10 @@ export {};
* key distributed equality semantics.)
*/

/**
* @typedef {Primitive | RemotableObject} ScalarKey
*/

/**
* @callback GetRankCover
* @param {Passable} payload
Expand Down
22 changes: 16 additions & 6 deletions packages/patterns/test/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import type { Passable } from '@endo/pass-style';
import { expectNotType, expectType } from 'tsd';
import { isKey } from '../src/keys/checkKey.js';
import { isKey, isScalarKey } from '../src/keys/checkKey.js';
import { M } from '../src/patterns/patternMatchers.js';
import type { Key } from '../src/types.js';
import type { Key, ScalarKey } from '../src/types.js';

// @ts-expect-error M.any missing parens
M.arrayOf(M.any);
M.arrayOf(M.any());

const passable: Passable = null as any;
{
const maybeKey: Passable = 'key';
const result = isKey(maybeKey);
const result = isKey(passable);
expectType<boolean>(result);
if (result) {
expectType<Key>(maybeKey);
expectType<Key>(passable);
} else {
expectNotType<Key>(maybeKey);
expectNotType<Key>(passable);
}
}
{
Expand All @@ -34,3 +34,13 @@ M.arrayOf(M.any());
someAny.foo;
}
}

{
const result = isScalarKey(passable);
expectType<boolean>(result);
if (result) {
expectType<ScalarKey>(passable);
} else {
expectNotType<ScalarKey>(passable);
}
}
Loading