1

I have a type that takes an object and marks one of its properties as being unique:

type UniqueString = string & { readonly UniqueString: unique symbol }

type HasUnique<A extends {}, K extends keyof A> = Omit<A, K> & Record<K, UniqueString>

I'm trying to write a type guard to take an A and return is HasUnique<A, K>. Here is my type guard, which gives me a type error:

declare const hasUnique: <A extends {}, K extends keyof A>(a: A) => a is HasUnique<A, K>

TS2677: A type predicate's type must be assignable to its parameter's type.   Type 'HasUnique<A, K>' is not assignable to type 'A'.     'HasUnique<A, K>' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype of constraint '{}'.

Is there a way to fix this, or is this one of those instances where I've hit the limitations of TS's inference with generics, as referenced in this SO question?

user1713450
  • 1,307
  • 7
  • 18

0 Answers0