Type introspection at runtime #686
-
This is more of a question so feel free to close. Is it possible to introspect at runtime the properties of a type instantiated with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
To provide at least partial answer to myself: the |
Beta Was this translation helpful? Give feedback.
-
You're looking for the Each type is internally represented using objects like this: const myType = type("number%2 | string<10")
// { number: {divisor: 2}, string: { limit: 10, comparator: "<" } }
console.log(myType.node) Internally, these nodes represent a full type system that can determine assignability between any two types, with utilities for doing things like extracting props. You can even create types directly from these node structures using a tuple expression with full static-inference! const t = type([
"node",
{
object: {
class: Array,
props: {
"[index]": { object: { props: { name: "string" } } }
}
}
}
])
type T = typeof t.infer
// ^? { name: string }[] Feel free to take a look at these tests for some more examples of various types and their node representations. We plan to expose and document those APIs in the near future, but in the short-term these representations should be relatively intuitive, so feel free to poke around and extract what you need. |
Beta Was this translation helpful? Give feedback.
You're looking for the
.node
prop!Each type is internally represented using objects like this:
Internally, these nodes represent a full type system that can determine assignability between any two types, with utilities for doing things like extracting props.
You can even create types directly from these node structures using a tuple expression with full static-inference!