-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: checked cast with TypedMatcher (#8394)
refs: #6160 ## Description I ran again into the need for type narrowing with a shape object: https://github.com/Agoric/agoric-sdk/pull/8385/files#diff-b17d46d065ac769cdf1d70471b16d141f28672965c18522d58d39722d4852ad4R329-R331 endojs/endo#1721 approached the general problem of inferring the type from the shape. But until we have that, we can at least move the type description onto the shape object so users of that shape can automatically get the type a match implies. With this, the code referenced above would be, ```js const questionDesc = cast(info, QuestionSpecShape); ``` ### Security Considerations n/a ### Scaling Considerations n/a ### Documentation Considerations Once this settles in agoric-sdk, we may want to move it into Endo. Alternately, Endo waits for more general support. ### Testing Considerations Has a test. Maybe should use tsd instead of Ava ### Upgrade Considerations n/a
- Loading branch information
Showing
50 changed files
with
299 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,6 +86,6 @@ | |
"access": "public" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 91.22 | ||
"atLeast": 91.23 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,6 @@ | |
"access": "public" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 75.02 | ||
"atLeast": 75.1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 76.99 | ||
"atLeast": 77.3 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 77.32 | ||
"atLeast": 76.95 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 91.11 | ||
"atLeast": 91.4 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 87.28 | ||
"atLeast": 86.66 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 74.36 | ||
"atLeast": 76.03 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 88.94 | ||
"atLeast": 88.92 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,6 @@ | |
"timeout": "20m" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 80.49 | ||
"atLeast": 80.6 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,6 @@ | |
"access": "public" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 81.63 | ||
"atLeast": 82.44 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,6 @@ | |
"access": "public" | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 89.31 | ||
"atLeast": 89.35 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// @ts-check | ||
import { mustMatch as typelessMustMatch } from '@endo/patterns'; | ||
|
||
/** | ||
* @import {MustMatch, PatternType, TypedPattern} from './types.js'; | ||
*/ | ||
|
||
/** @type {MustMatch} */ | ||
export const mustMatch = typelessMustMatch; | ||
|
||
/** | ||
* @template M | ||
* @param {unknown} specimen | ||
* @param {TypedPattern<M>} patt | ||
* @returns {M} | ||
*/ | ||
export const cast = (specimen, patt) => { | ||
// mustMatch throws if they don't, which means that `cast` also narrows the | ||
// type but a function can't both narrow and return a type. That is by design: | ||
// https://github.com/microsoft/TypeScript/issues/34636#issuecomment-545025916 | ||
mustMatch(specimen, patt); | ||
return specimen; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// @ts-check | ||
import test from 'ava'; | ||
|
||
import { makeExo } from '@endo/exo'; | ||
import { M } from '@endo/patterns'; | ||
import { cast, mustMatch } from '../src/typeCheck.js'; | ||
|
||
/** | ||
* @import {TypedPattern} from '@agoric/internal'; | ||
* @import {RemotableObject} from '@endo/marshal'; | ||
*/ | ||
|
||
const Mstring = /** @type {TypedPattern<string>} */ (M.string()); | ||
const MremotableFoo = /** @type {TypedPattern<RemotableObject<'Foo'>>} */ ( | ||
M.remotable('Foo') | ||
); | ||
const MremotableBar = /** @type {TypedPattern<RemotableObject<'Bar'>>} */ ( | ||
M.remotable('Bar') | ||
); | ||
|
||
const unknownString = /** @type {unknown} */ (''); | ||
|
||
test('cast', t => { | ||
// @ts-expect-error unknown type | ||
unknownString.length; | ||
// @ts-expect-error not any | ||
cast(unknownString, Mstring).missing; | ||
cast(unknownString, Mstring).length; | ||
t.pass(); | ||
}); | ||
|
||
test('mustMatch', t => { | ||
// @ts-expect-error unknown type | ||
unknownString.length; | ||
mustMatch(unknownString, Mstring); | ||
unknownString.length; | ||
t.pass(); | ||
}); | ||
|
||
test('remotable', t => { | ||
const maybeFoo = makeExo(`Remotable1`, undefined, {}); | ||
mustMatch(maybeFoo, MremotableFoo); | ||
maybeFoo; // narrowed to Foo | ||
|
||
const maybeBar = makeExo(`Remotable2`, undefined, {}); | ||
mustMatch(maybeBar, MremotableBar); | ||
maybeBar; // narrowed to Bar | ||
|
||
mustMatch(maybeFoo, MremotableBar); | ||
maybeFoo; // further narrowed to never | ||
mustMatch(maybeBar, MremotableFoo); | ||
maybeBar; // further narrowed to never | ||
|
||
t.pass(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,6 @@ | |
"workerThreads": false | ||
}, | ||
"typeCoverage": { | ||
"atLeast": 89.7 | ||
"atLeast": 90.69 | ||
} | ||
} |
Oops, something went wrong.