fsharp / fsharp/fslang-suggestions
Literals as types
- Dominant language
- No language data
- Stars
- 373
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
I propose we implement something like what's known as [Literal Types in TypeScript](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types):
```ts
const foo = (bar: "A" | "B") => {}
foo("A"); // ok
foo("C"); // forbidden
```
However, as in TypeScript, it should not be limited to literals. In fact, TS accepts any type here:
```ts
interface Options {
width: number;
}
function configure(x: Options | "auto") {}
configure({ width: 100 }); // ok
configure("auto"); // ok
configure("automatic"); // forbidden
```
As someone who is actively working on an application for the last couple of months which uses TS for the frontend and F# for the backend, I find myself often missing this capability when switching from TS to F#. Right now, I have to explicitly define a discriminated union to do that in F#:
```fs
type Bar = A | B
let foo (bar: Bar) = ()
foo A
```
Instead, it would be really neat to define that DU "on the fly" or "inline" (not sure what the proper wording might be) as in TypeScript:
```fs
let foo (bar: A | B) = ()
```
## Pros and Cons
Advantages: More flexibility, I guess.
Disadvantages: As always, it's not a silver bullet. There are plenty of reasons one may want to define the type of that parameter explicitly, e.g., to reuse it elsewhere.
## Affidavit
Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.
Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [x] I or my company would be willing to help implement and/or test this
## For Readers
If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reading issue #1195 and its 32-comment discussion, including the TypeScript literal-type examples and the proposed inline F# syntax. The desired outcome is an agreed design for inline unions that supports literals and other types, with implementation and validation consistent with that design.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fsharp, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100