Skip to content

Commit 537db10

Browse files
authored
Document branded strings in the compiler
See [here](microsoft/TypeScript#16915 (comment))
1 parent b4346b8 commit 537db10

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Typescript-compiler-implementation.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,28 @@ ES2015. Then the emitter pretty-prints the AST.
473473

474474
TODO: This leaves out module transformers.
475475

476+
## Branded Types
477+
478+
Two branded types exist within the compiler, `Path` and `__String`. These
479+
are both branded strings with different shapes and different purposes.
480+
481+
`Path` is used to represent an absolute, normalized, canonical folder path
482+
or file name. If you have a `Path`, it should already be all of the above.
483+
You may get a path from a path-looking string via `toPath`, and a `Path` may
484+
be safely used anywhere a string is requested.
485+
486+
`__String` is used to represent a string whose leading underscore have been
487+
escaped (if present) or a string representing an internal compiler symbol name,
488+
such as `"__call"`. To escape a string in this way, call `escapeLeadingUnderscores`.
489+
To unescape such a string, call `unescapeLeadingUnderscores`. Strings of this kind
490+
are used to represent symbol names and identifier text, to allow for internal symbol
491+
names to cohabitate the same symbol table as normal members without being in conflict.
492+
The brand on this type is structured such that it is castable and comparable with
493+
normal strings, but a normal string cannot be used in its place and it can not
494+
be used in place of a normal string (to prevent escaped text from leaking out via
495+
diagnostic messages and the like). The union member enabling this is the member intersected
496+
with `void`, which makes it incompatible with anything asking for a string.
497+
476498
## Services
477499

478500
I don't know anything about services. And besides, this readme is for

0 commit comments

Comments
 (0)