11.09.2015 Views

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

Typescript Deep Dive by Basarat Ali Syed

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TypeScript <strong>Deep</strong> <strong>Dive</strong><br />

AST Tip: SyntaxKind<br />

SyntaxKind is defined as a const enum , here is a sample:<br />

export const enum SyntaxKind {<br />

Unknown,<br />

EndOfFileToken,<br />

SingleLineCommentTrivia,<br />

// ... LOTS more<br />

It's a const enum (a concept we covered previously) so that it gets inlined (e.g. ts.SyntaxKind.EndOfFileToken becomes 1 )<br />

and we don't get a dereferencing cost when working with AST. However the compiler is compiled with --<br />

preserveConstEnums compiler flag so that the enum is still available at runtime. So in JavaScript you can use<br />

ts.SyntaxKind.EndOfFileToken if you want. Additionally you can convert these enum members to display strings using the<br />

following function:<br />

export function syntaxKindToName(kind: ts.SyntaxKind) {<br />

return (ts).SyntaxKind[kind];<br />

}<br />

TIP: SyntaxKind enum<br />

76

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!