I have a class which has some mandatory properties and can then have other properties defined by classes extending this one (or anyway, some extra properties I don't know about).
Now I'm trying to use Omit
type on it, defined as
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
If we define the class as
class Thing {
constructor(
public id: number,
public name: string,
public description: string
) {}
}
We can do Omit<Thing, 'id'>
and get the right type inference as Pick<Thing, "name" | "description">
If we add a [index: string]: any
to the class to support unknown properties and retry Omit<Thing, 'id'>
, now the inferred type will be Pick<Thing, string | number>
.
class Thing {
constructor(
public id: number,
public name: string,
public description: string
) {}
[index: string]: any;
}
This doesn't really make sense: when I type using the class, all properties are required even when a [index: string]: any;
is defined, why would it become a looser type when omitting one of its properties? I would expect the return type to still be Pick<Thing, "name" | "description">
.
Does anyone knows what's going on?
See Question&Answers more detail:os