Currently, I am in the process of developing code that automatically generates type hints based on function calls related to GraphQL Nexus tasks.
In one of its functions, it requires an anonymous type constructed from the attributes generated automatically:
export const Item = {
isTypeOf(data) { // The type of data is anonymous, for example '{ name: string }'
...
}
definition(t) {
t.string('name')
}
}
However, the data
parameter might contain additional variables beyond those specified by the function calls. In my specific case, I need to access a property called kind
, but invoking the t._type_
function could lead to unintended consequences.
I cannot just pass the type as { kind: string }
since the isTypeOf
method expects it to include at least all the properties defined in its arguments.
While I could use { name: string, kind: string }
in this scenario, my real code involves more intricate objects, and using this approach would negate the advantages of automated typing functionality.
Is there any way for me to extend an argument inline with an anonymous type? One idea I had was to introduce a keyword such as initial
or default
to inherit an argument's original type, like so:
isTypeOf(data: initial & { kind: string })
isTypeOf(data: default & { kind: string })