Skip to content

Instantly share code, notes, and snippets.

@eterps
Last active April 19, 2019 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eterps/d7ee5525825ae13c24e818b0614d150e to your computer and use it in GitHub Desktop.
Save eterps/d7ee5525825ae13c24e818b0614d150e to your computer and use it in GitHub Desktop.
type CustomerId = number
type OrderId = number
let customerId: CustomerId
let orderId: OrderId
customerId = 42
orderId = 42
if (customerId == orderId) {
console.log('Typescript says they are equal')
}
// 2nd attempt:
enum CustomerIdBrand {}
type CustomerId2 = CustomerIdBrand & number
enum OrderIdBrand {}
type OrderId2 = OrderIdBrand & number
let customerId2: CustomerId2
let orderId2: OrderId2
customerId2 = 42
orderId2 = 42
if (customerId2 == orderId2) {
console.log('Typescript says they are equal?!')
}
// 3rd attempt:
interface CustomerId3 {
_customerIdBrand: void
value: number
}
interface OrderId3 {
_orderIdBrand: void
value: number
}
let customerId3: CustomerId3 = { value: 42 } as CustomerId3
let orderId3: OrderId3 = { value: 42 } as OrderId3
// error TS2365: Operator '==' cannot be applied to types 'CustomerId3' and 'OrderId3'
// if (customerId3 == orderId3) { console.log('Finally!') }
let {value} = customerId3
console.log(value)
let {value: innerValue} = customerId3
console.log(innerValue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment