Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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