Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A nullable type #105

Open
samhh opened this issue Apr 6, 2022 · 6 comments
Open

A nullable type #105

samhh opened this issue Apr 6, 2022 · 6 comments

Comments

@samhh
Copy link
Member

samhh commented Apr 6, 2022

Some context: #102 (comment)

@samhh
Copy link
Member Author

samhh commented Apr 14, 2022

Copied from the linked issue:


That's interesting. I suppose it'd be analagous to other. Where other widens e.g. 'a' | 'b' to string (encoded conveniently via a union), null could widen/unionise to null. Not just in select, but also plural.

Ideally though a syntactic construct for null could apply to any type, otherwise we can't handle cases like Date | null. I think that's where the complexity I'd previously considered comes into play, it incorporates a sort of polymorphism (A | null).

If we say hypothetically the implementation would be doable, how about this?:

now being reviewed {topicTitle, nullable, null {by the community} other {in the {topicTitle}}}

# compiles `topicTitle` to `string | null`

Or a motivating example for the broader nullable type:

You joined {joined, nullable, null {a while ago} other {on {joined, date, long}}}

# compiles `date` to `Date | null`

@samhh
Copy link
Member Author

samhh commented May 1, 2022

How could we prevent the following without introducing our own typechecking?:

{x} {x, nullable, null {} other {{x}}}

In the above design x is probably string | null however that makes the first interpolation unsafe.

@OliverJAsh
Copy link
Member

OliverJAsh commented May 3, 2022

@samhh In theory this could be caught by running @typescript-eslint/restrict-template-expressions on the output code? We have that rule enabled in unsplash-web but IIRC we don't currently lint the generated TS.

@samhh
Copy link
Member Author

samhh commented May 3, 2022

@OliverJAsh I don't think we can leave this for userland. This'd be a type error on output:

{x, number} {x, nullable, null {} other {}}

Where number | null would be fed to Intl.NumberFormat.

@OliverJAsh
Copy link
Member

@samhh Perhaps it's OK to have a type error on output? I guess it's a trade-off: defer validation to TS/tsc vs provide validation upfront.

@samhh samhh removed the discussion label Jun 23, 2022
@samhh
Copy link
Member Author

samhh commented Sep 7, 2022

I think the above would actually compile to number via intersection of arguments, making it safe.

string & (string | null) = string

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants