3 ms·
Literal types quite simply means that values are types. For example in TypeScript the literal type "Foo" is a special string type, where the only valid value is
by paavohtl 5y ago
Literal types quite simply means that values are types. For example in TypeScript the literal type "Foo" is a special string type, where the only valid value is the literal "Foo" – assigning "Bar" to a variable with the type "Foo" is a compile time type error. Literal types can be combined into union types like any other type.
- cutler 5y agoSounds like more Scala sophistry. I thought they were supposed to be aiming for pragmatism with Scala 3.
- esarbe 5y agoI can think of plenty cases where I would want to limit the input space of a function to a known set of literals. Just as an example; the `document.getContext` [0] api takes a string argument to select the context canvas. Without literal types, you cannot enforce this to a set of valid values. Literal types gives you the power to tell the compiler, which values are valid. Since there are other programming languages that support literal types, it's not just Scala sophistry. :-P [0] https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/getContext https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasE...
- nsonha 5y agothat's exactly how it works in TS. You can call TS sophistry then. Most people I talk to put it on the pragmatic side
- doikor 5y agoThis is just making clear and easy to use something that people did using some type level magic (see shapeless Witness). Something that people used through ugly hacks becoming part of the language natively in a easy to use and understand (and faster to compile) way sounds very much like pragmatism to me. Here is the source code to Witness (and other singleton stuff in shapeless) https://github.com/milessabin/shapeless/blob/main/core/src/main/scala/shapeless/singletons.scala https://github.com/milessabin/shapeless/blob/main/core/src/m...
- crucialfelix 5y agoI use it in Typescript often. Very useful. type Verb = 'GET' | 'POST' | 'PUT' | 'DELETE'; export type TerminusType = 'ICAO' | 'IATA' | 'LOCODE' | 'ADDRESS' | 'LATLNG'; It's much better than just assuming it's a string and hoping you don't make a typo. If the value is supplied from user input then it ensures that you write checks or switch cases.
- growse 5y ago> I use it in Typescript often. Very useful. > type Verb = 'GET' | 'POST' | 'PUT' | 'DELETE'; > export type TerminusType = 'ICAO' | 'IATA' | 'LOCODE' | 'ADDRESS' | 'LATLNG'; > It's much better than just assuming it's a string and hoping you don't make a typo. > If the value is supplied from user input then it ensures that you write checks or switch cases. Conceptually, how is this different to an enum?
- acjohnson55 5y agoIt can be used as an enum and might be more convenient in places where you don't need to bother converting between string data outside the system and enum data types. It also plays nicely with union types, so that you can very quickly define as hoc enums directly in your function declarations, which might be useful if there's only one or two usages of a particular enum, or a section of code where the allowable values very quickly changes. You basically get a lot of expressiveness without boilerplate, which can be pretty convenient.
- crucialfelix 5y agoAn Enum in ts is weird. It's not just a type, it's something compiles to something in the runtime. So to use the value you have to import the Enum and use that. const handlers = { '/': { Verbs.GET: () => .... Verbs.POST: () => ... Whereas: const handlers = { '/': { GET: () => .... PAWST: //nope! must be one of GET | POST | PUT | DELETE Basically it's a lot less work and you don't need to import the enum and maintain it.
- stickfigure 5y agoConceptually, it's very much like an enum. Except that you can't enumerate them at runtime because there is no runtime representation. This can be very frustrating. There's a hack, it looks like: export const VERBS = ['GET', 'POST', 'PUT', 'DELETE'] as const; type VerbTuple = typeof VERBS; export type Verb = VerbTuple[number]; A real enum would be better, but would violate the "typescript is just javascript" rule.
- mjburgess 5y agoThey're describing typescript...