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

Add support for exactOptionalPropertyTypes TypeScript option #77

Merged
merged 1 commit into from
May 9, 2024

Conversation

keijokapp
Copy link

TypeScript 4.4 introduced exactOptionalPropertyTypes compiler option. This fixed one of the major issues I had with TypeScript and, I think, should be the default in all new projects. It avoids unnecessary checks and type assertions/casts, improving type safety. But it also means that all optional object field types in the project, including those from third-party libraries, have to explicitly include undefined to preserve the old behavior.

This PR is mostly just convenience, so I could do

function foo(reverse?: boolean) {
	return tn.getRange('', '\xff', { reverse });
}

instead of

function foo(reverse?: boolean) {
	return tn.getRange('', '\xff', reverse != null ? { reverse } : {});
}

This PR:

  • Adds undefined to optional object field types where applicable (which is most of them).
  • Adds exactOptionalPropertyTypes: true to TypeScript configuration.
  • Removes boilerplate from tsconfig.json because it's weird to add options with the boilerplate in the way.

While I'm at it, would that be ok to do some maintenance tasks too? Like:

  • Fix excess whitespaces at the end of lines. Most development environments should do this automatically, so excess whitespaces cause some pain.
  • Fix trailing new lines to make Git and other tools happy. Again, most development environments should do this automatically.
  • Update dependencies
  • Possibly add a basic ESLint setup

@josephg
Copy link
Owner

josephg commented Apr 25, 2024

Looks good. However, it looks like you've manually edited opts.d.ts - and that file should be autogenerated from one of the scripts. Any manual changes will be lost like this. Do you mind updating the generation script in scripts/gentsopts.ts while you're at it?

@keijokapp keijokapp force-pushed the exact-optional-property-types branch from 4a29361 to a8c3341 Compare April 25, 2024 11:04
@keijokapp
Copy link
Author

Forgot about that. Done.

@keijokapp keijokapp force-pushed the exact-optional-property-types branch from a8c3341 to 7802fdb Compare April 25, 2024 11:10
@keijokapp keijokapp force-pushed the exact-optional-property-types branch from 7802fdb to 45c1d1c Compare May 5, 2024 08:08
@josephg josephg merged commit 81304b8 into josephg:master May 9, 2024
3 checks passed
@keijokapp keijokapp deleted the exact-optional-property-types branch May 9, 2024 13:49
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

Successfully merging this pull request may close these issues.

2 participants