Repository navigation
Best way to use attest to generate TS type as a string #1541
|
Hi, I find attest's type serialisation capabilities extremely reliable, and I use (abuse) them often to debug, explore new code or help to write documentation. Right now, I'm using the following snippet to get the full string representation of a runtime value: import { attest, setup } from "@ark/attest";
setup();
console.log(attest({ a: "a", b: "b" }).type.toString.unwrap());Is there a way to tap inside attest to only use the serialisation part? I tried to find it myself, but I'm struggling to fully understand the assertion-chaining code and its relationship to type inference. |
Replies: 1 comment 1 reply
|
I dug into the actual exports to see if there's a lower entry point than the full assertion chain, and there isn't really one, Everything under So Practical implication for your use case: keep using |
I dug into the actual exports to see if there's a lower entry point than the full assertion chain, and there isn't really one,
attest(value).type.toString.unwrap()already is the minimal supported path for this.Everything under
.typeis powered bygetTypeAssertionsAtPosition(exported from@ark/attest'scache/getCachedAssertions), which is keyed by source position, not by the runtime value.setup()runs a TypeScript Language Service pass over the whole file once, resolves the type at everyattest(...)call site, and caches it (writeAssertionDataat teardown). When you read.type.toString, it just looks up the cache entry for the positioncaller()captured at the moment you invokedattest.…