Typescript utility type problem with unknown properties
03:15 11 Nov 2025

I am trying to build a utility type that keeps known properties and they're type. I should be able to set also other keys but only as string and avoid using keys that are Callable.

Here is what I did so far:

type MyType = {
  [K in string]?: K extends keyof T ? (T[K] extends Callable ? never : T[K]) : string;
};

interface A {
  foo: string;
  bar: number;
  callMe(): void;
}

const test: MyType = {
  bar: 42, // This should work but it's not: Type 'number' is not assignable to type 'string'
  callMe: () => {}, // ✅ TypeScript error: Works fine because of the Callable
  customAttr: 'customValue', // ✅ Unknow keys should be string value
  foo: 'hello', // ✅ Known keys should follow the type
};
typescript