Typescript index signature with known properties of mixed types

Viewed 147

How to specify an index signature with know properties of fixed type and all other properties of a different type. The following example behaves as expected with as any but fails when all properties are specified:

type S = {
    s?: number;
} 
type H = {
    [key: string]: Function | undefined;
}
type Handler = S & H


const h1: Handler = {} as any;

/* Ok */
h1.sadwa?.()
h1.s = 2;


/* Not ok */
const h2: Handler = {
    s:  22,
    f: ()=>{}
}
h2.f()
h2.s = 2;

https://www.typescriptlang.org/play?#code/C4TwDgpgBAylC8UDeAoK6oGcD8AuKAdgK4C2ARhAE4DcKAvlCqJFABILJoYDaA1hCHyZglAJYEA5gF18AMSIEAxsFEB7AlAA+UBQBMIAM3ERdtOk3DRWAQwK6ANlQ5wAZGxQfF64VAAWARnwbO0dKDiQGa0woWxBaFAB6ACooAHleKCSElACAOkxrXQB3a2xcgAoAShz-fI4AJnjElIA5VWAoVQyslC8CH196oNsHJ0RUDCx8KHr6gBoudAN8KvgAPgj6HPrcgyrtusRGoA

0 Answers
Related