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

Enable --exactOptionalPropertyTypes #14

Open
sindresorhus opened this issue Aug 26, 2021 · 4 comments
Open

Enable --exactOptionalPropertyTypes #14

sindresorhus opened this issue Aug 26, 2021 · 4 comments

Comments

@sindresorhus
Copy link
Owner

sindresorhus commented Aug 26, 2021

I don't want to do it yet as it will cause some friction. We can try to enable it in 2023.

https://devblogs.microsoft.com/typescript/announcing-typescript-4-4/#exact-optional-property-types

@kidonng
Copy link

kidonng commented Mar 13, 2023

Almost 3 months into 2023, is it time yet?

@fregante
Copy link
Contributor

fregante commented Mar 13, 2023

I think this will make conditional properties annoying to set:

const a: A = {
	name: 'Fregante',
	email: user.hasEmail ? user.email : undefined
}

Would need to be:

const a: A = {
	name: 'Fregante',
	...(user.hasEmail ? {email: user.email} : undefined)
}

Or worse yet split with an if.

JavaScript badly needs a new nullish value 🤭

const a: A = {
	name: 'Fregante',
	email: user.hasEmail ? user.email : reallyundefined
}

@kidonng
Copy link

kidonng commented Mar 27, 2023

TS ESlint has issues with exactOptionalPropertyTypes: typescript-eslint/typescript-eslint#5344

@voxpelli
Copy link

@fregante It only makes it annoying to set if the type hasn't been explicitly set to accept undefined, though it makes other cases less annoying, like:

type A = {
  email?: string
}

const foo: A = {
  email: 'foo@example.com'
}

if ('email' in foo && foo.email.length > 1) {
  console.log('This only works with exactOptionalPropertyTypes');
}

When it comes to module development its good to have exactOptionalPropertyTypes on as it will ensure that one specifies types that are not annoying to use with exactOptionalPropertyTypes, though its also dangerous as the above example would throw if someone were to send in an explicit undefined because they lacked exactOptionalPropertyTypes.

I personally have exactOptionalPropertyTypes in all my projects, including my modules.

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

No branches or pull requests

4 participants