I am trying to find the syntax to declare a type that has at least these properties, for example:
interface InspectedError {
originalErrorObj: any,
errorAsString: string,
errorObjParsed: {
message: string,
stack: string
}
}
the errorObjParsed value might have more properties, should I do this:
interface InspectedError {
originalErrorObj: any,
errorAsString: string,
errorObjParsed: {
message: string,
stack: string,
...any
}
}
there is the Partial<> construct, I am looking for like Subset<> or Superset<> construct, like this:
interface InspectedError {
originalErrorObj: any,
errorAsString: string,
errorObjParsed: SupersetOf<{
message: string,
stack: string,
}>
}
so that I could do:
const e = new Error('test');
e.foo = 'bar';
const v: InspectedError = {
originalErrorObj: e,
errorAsString: util.inspect(e),
errorObjectParsed: {
message: e.message,
stack: e.stack,
...e
}
}
and allow for any other arbitrary properties that might exist on e
.
2
Answers
There are various ways of achieving this, but assuming you want to avoid one involving
class
es and subclass
es, I’d suggest:You could create the
SuperSet<>
construct you asked for using an index signature like this. This tells TypeScript to assume thaterrorObjParsed
could have any properties besidesmessage
andstack
. The only problem is that accessing properties that don’t actually exist on some hypotheticale
will not give an error, and you won’t get code completion for anything besidesmessage
andstack
.Alternatively
If you make
InspectedError
a generic type, then you can makeerrorObjParsed
an intersection type. With this, you can safely accessmessage
,stack
, and the properties ofe
.