Try it Live
Run ENS examples in the interactive playground
Branded Ens Type
Definition
Ens is a plain JavaScript string at runtime. The brand exists only in the type system, so TypeScript rejects an arbitrary string where an Ens is required. The source type is EnsType in src/primitives/Ens/EnsType.ts; the module exports it as Ens.
What the brand guarantees
The brand records how a value was produced, not that it is valid. Each producer gives a different guarantee:Constructors and conversion
from(name)
from() for names you already normalized, for example names read from your own database, or to call the internal _normalize, _beautify, _namehash, and _labelhash functions.
normalize(name)
DisallowedCharacterError on invalid names. See Normalization.
toString(name)
string, dropping the brand.
Ens extends string, you can also pass it anywhere a string is accepted without calling toString().
is(value)
unknown to Ens when value is a non-empty string. See Validation.
Public wrappers and internal functions
Each operation has a public wrapper that acceptsstring | Ens and an internal function that takes Ens only:
The wrappers call
from(String(name)) and then the internal function. The results are identical.
Factory functions
Namehash and Labelhash build hash functions from an injected keccak256 implementation:
Ens, not string. See Encoding.
See Also
- ENS Overview
- Branded Types - Voltaire’s branding pattern
- Normalization
- Validation
- Encoding

