Skip to content

Instantly share code, notes, and snippets.

@balanza
Last active January 31, 2019 15:33
Show Gist options
  • Save balanza/9bb9e7c792c41e826df86e631e556f68 to your computer and use it in GitHub Desktop.
Save balanza/9bb9e7c792c41e826df86e631e556f68 to your computer and use it in GitHub Desktop.
type ConfirmedMobile = string & { __phantom_1548946378096: never }
const createConfirmedMobile = (num: String) => createOf<ConfirmedMobile, String>(num)
type CertifiedMobile = string & { __phantom_1548946391842: never }
const createCertifiedMobile = (num: String) => createOf<CertifiedMobile, String>(num)
const confirmed = createConfirmedMobile('2984829202')
// ^^^ ConfirmedMobile
const certified = createCertifiedMobile('2984829202')
// ^^^ CertifiedMobile
const wrongInput = createCertifiedMobile(1234332)
// ^^^ Compilation error: Argument of type '1234332'
// is not assignable to parameter of type 'String'.
confirmed == certified
// ^^^ This condition will always return 'false'
// since the types 'ConfirmedMobile' and
// 'CertifiedMobile' have no overlap.
confirmed === certified
// ^^^ This condition will always return 'false'
// since the types 'ConfirmedMobile' and
// 'CertifiedMobile' have no overlap.
const anotherCertified: CertifiedMobile = confirmed
// ^^^ compilation error: different types
console.log(typeof confirmed === "string")
// ^^^ true
console.log(typeof confirmed)
// ^^^ "string"
const both = confirmed + '/' + certified
// ^^^ "2984829202/2984829202", because both are type string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment