sandersn / mini-typescript

A miniature model of the Typescript compiler, intended to teach the structure of the real Typescript compiler
MIT License
1.14k stars 61 forks source link

Clarification on `String` literals #15

Closed imteekay closed 1 year ago

imteekay commented 1 year ago

Hi @sandersn, I'm working on the third exercise "Add string literals".

Does the exercise require adding type-checking to the string literals too? Or adding a token and parsing it is enough?

This is the wip PR: https://github.com/imteekay/mini-typescript/pull/4

Edit: I'm studying how the type checker works and was able to type check string literals using typeof for the expression values (https://github.com/imteekay/mini-typescript/pull/4/commits/a1df2c712338ca532dfc04a017e9deb7db03dd35). I also complemented the singleTypedVar test to cover this implementation. But still not sure if using the typeof is the right approach here.

sandersn commented 1 year ago

The exercise is open-ended, although it'll work better if you assign some type to string literals. For a miniature compiler, the typeof approach you use is fine -- you're re-using the representation of strings from Javascript so you can reuse typeof. For a full-size compiler, you'll want to add a field when you parse a literal that tracks its kind.

imteekay commented 1 year ago

Thanks for all the help 🙇 just merged the PR