#Enforce types to NOT have specific keys?

6 messages · Page 1 of 1 (latest)

brave portal
#

Let's say I have this interface...

interface User {
  uuid:             string,
  username:         string,
  favoriteColor:    Color,
  creditCardNumber: CardNumber,
}

Obviously, I'm not storing credit card numbers like this, but you get the idea. There's some public data, and then data I don't want to be passed around.
How can I create a type that matches User's keys EXCEPT creditCardNumber?

I currently have something like this, but I'm not entirely sure if it's correct:

type PublicDataOnly = Omit<User, "creditCardNumber">;

If an object that has a creditCardNumber key is passed into a function that accepts PublicDataOnly, it should throw a compile-time error. Is Omit<> the correct way to do this, or is there some other wrapper type I should use?

pliant ore
#

Omit<User, K> & Partial<Record<K, never>>

obtuse valley
#

Even that isn't a perfect guarantee that the passed object will not have the field.

#

I suggest being defensive in the runtime code so extra properties don't cause issues, personally

brave portal
#

oh forgot to do this

#

!resolved