search

Home  >  Q&A  >  body text

Assert that a type is not branded

<p>If we define a type brand, for example: </p> <pre class="brush:php;toolbar:false;">declare const nominalSymbol: unique symbol; type Nominal<T extends string, U> = U & { [nominalSymbol]: T };</pre> <p>Is there a way to define a type <code>NotNominal<U></code> that resolves to <code>U< if <code>U</code> is not a brand type /code>. </p> <pre class="brush:php;toolbar:false;">declare const nominalSymbol: unique symbol; type Nominal<T extends string, U> = U & { [nominalSymbol]: T }; type BrandedType = Nominal<'Address', string>; type a = NotNominal<string> // This should be `string` type b = NotNominal<Address> // This should be `never`</pre> <p><br /></p>
P粉323224129P粉323224129525 days ago556

reply all(1)I'll reply

  • P粉727531237

    P粉7275312372023-08-19 10:27:26

    type NotNominal<U> = U extends { [nominalSymbol]: string } ? never : U

    reply
    0
  • Cancelreply