0

Is there a way to ensure type structure when combining with any?

Here is an example what I have in mind:

function foo<T extends object>(obj: T): { type: 'FooBar' } & T {
    return { type: 'FooBar', ...obj };
}

const bar = foo({bar: 'foo'} as any); // expecting bar type to be {type: 'FooBar'; [x: string]: any;} not any

Playground Link

zimex
  • 713
  • 1
  • 5
  • 10
  • 2
    No, that's the point of `any`; it is considered compatible with any type whatsoever. Don't use `any` if you want type safety. Can you elaborate on why you are trying to do this? – jcalz Apr 30 '20 at 14:05
  • Possible duplicate of [Type intersections using any](https://stackoverflow.com/questions/46673558/type-intersections-using-any/46673732#46673732) – jcalz Apr 30 '20 at 14:07

0 Answers0