-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Optional types syntax (2 defs: w/ type
and w/ inline definition) - closed prematurely
#51883
Comments
Appreciate the write-up, but I do think that we've discussed these before. The only thing I could see us doing to revisit binding name optionality is for the global scope (think Node's |
@MartinJohns @DanielRosenwasser I haven't come across any tickets—still—that this ticket duplicates. Looking at two seemingly related tickets—
This proposal I'm submitting here does not overlap with either of those, as this relates to top-level variables or types in a given scope, though not necessarily global. There is no way to achieve this currently without explicitly writing <script lang="ts">
export let prop1: string | undefined = undefined;
export let prop2: number | undefined = undefined;
export let prop3: string[] | undefined = undefined;
export let prop4: { prop: number } | undefined = undefined;
export let prop5: string | null | undefined = undefined;
</script> ☝🏼🤮 <script lang="ts">
export let prop1?: string;
export let prop2?: number;
export let prop3?: string[];
export let prop4?: { prop: number };
export let prop5?: string | null;
</script> ✨ …though the |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
type
and w/ inline definition)type
and w/ inline definition) - closed prematurely
Suggestion
🔍 Search Terms
type optional syntax variable definition
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
I think it could be very useful to be able to declare optional types in TypeScript in a more clear and consise way. This proposal is two-fold— one syntax for defining a
type
and one syntax for inline typing on a variable.type
usage, I propose a shorthand syntaxTypeName ?= Type
which would match for the specified type orundefined
, where the?=
indicates that it is optional and can match either the specified type orundefined
.let variableName ?: Type
which would type the variable as the specified type orundefined
, where the?:
indicates that the variable is optional and can either be of the specified type orundefined
.This syntax would provide a more concise and readable way to create optional variables, as opposed to using the
| undefined
syntax which can be somewhat unwieldy, especially when creating a handful of variables you want toi be optional within a framework.This would make it easier to clearly communicate the intended use of a variable to other developers reading your code.
📃 Motivating Example
You could use the optional
type
syntax to create an optionaltype
and then use it like this:You could use the optional inline variable syntax to create an optional
type
on the variable like this:…but what if someone uses both?
It's plausible that in some cases, a user may use the optional inline syntax using a type that already uses the optional
type
syntax, or create unions or intersections using some optional and other non-optional types.For union types, that could look something like this:
In these cases, I think it woulkd make the most the most sense just to let any types declared with the an optional declaration be shorthand for their same usage as
Type | undefined
, so in the above example, the type forpersonalDetail
would evaluate tostring | undefined | number
.For intersection types, the type would usually evaluate to
never
, the same that it would if union-ing withundefined
, like in this example:💻 Use Cases
Working within Svelte, for example, it's not uncommon to see TypeScript used like this when defining props:
With this proposal implemented, this block could be rewritten to the following:
The space here would be optional, as always, so this would produce the same result:
The text was updated successfully, but these errors were encountered: