Skip to content

restore type inference & type safety to ts defs#98

Merged
af merged 3 commits into
af:masterfrom
lostfictions:master
Apr 3, 2019
Merged

restore type inference & type safety to ts defs#98
af merged 3 commits into
af:masterfrom
lostfictions:master

Conversation

@lostfictions

@lostfictions lostfictions commented Mar 23, 2019

Copy link
Copy Markdown
Contributor

This PR reverts the problematic changes in #83, as discussed there.

Instead of changing the type of env back to any, I changed it to the more recently introduced unknown type, which has somewhat better properties for type safety when used as a return value. The only caveat to this is that it raises the minimum required Typescript version for consuming these definitions to 3.0, which may be acceptable.

Comment thread src/envalid.d.ts Outdated

@af af left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, granted I'm not a TS user. Would it be possible to create a small runnable TS file in example/ that exercises the changes here? It'd make it a lot easier to verify any regressions in the typings moving forward.

Comment thread src/envalid.d.ts
// returned environment object can have properties other than the ones we've
// validated. these are not parsed or processed, and thus are always of type
// `string`. If you need better type safety and a fully-inferred environment,
// use `cleanEnv` in strict mode.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this comment! Makes me wonder if strict checking should have its own top-level cleanEnvStrict() so the typings could me more exact though

Comment thread src/envalid.d.ts
* A function used to transform the cleaned environment object before it is returned from cleanEnv.
*/
transformer?: (env: CleanEnv) => CleanEnv
transformer?: (env: unknown) => unknown

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just read up on the new unknown type, pretty cool! I'm fine with bumping up the required typescript version with this change, probably means the next release should be a major since TS is becoming so widely used

@lostfictions

Copy link
Copy Markdown
Contributor Author

@af done! i also amended the unfinished sentence in the existing example 😄

re: verifying regressions, there's also the existing typescript test file, which i've already adjusted for this PR. but as i noted in #83 (comment), it's difficult (and maybe ultimately undesirable) to make an example of invalid typescript code that tests exactly the properties we want to verify. for what it's worth, feel free to ping me on further typescript PRs if you'd like my input :)

@af af merged commit a039d3b into af:master Apr 3, 2019
@af

af commented Apr 3, 2019

Copy link
Copy Markdown
Owner

Thanks for adding the example, it really helps to be able to easily try and verify the inference and type-checking. This looks good and works well, merged! Do you think this should require a major version bump, due to the type definition changes and increased TS version requirement?

@lostfictions

Copy link
Copy Markdown
Contributor Author

yeah, i think so! even without the increased TS version requirement, this restores strict type checking for cleanEnv's strict mode, so it may eg. cause someone's builds to fail (for good reasons, but it should probably be opt-in nonetheless).

@lostfictions

Copy link
Copy Markdown
Contributor Author

@af incidentally, any chance you'd be up for cutting a new release sometime soon? :)

@af

af commented Apr 11, 2019

Copy link
Copy Markdown
Owner

Yes, will do, just wanted to take a little time to see if there were any other changes to make before cutting a major. Regardless, planning on releasing that in the next few days, sorry for the delay!

@af

af commented Apr 12, 2019

Copy link
Copy Markdown
Owner

v5.0.0 released! Let me know if you run into any issues

@lostfictions

Copy link
Copy Markdown
Contributor Author

thanks so much ❤️

tuannm151 pushed a commit to BSSCommerce/shopify-envalid that referenced this pull request Jul 1, 2024
* restore type inference & safety to ts defs

* widen return type of non-strict cleanenv

* add typescript example
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.

3 participants