Skip to content

never properties should not require the properties to be present #43954

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

Closed
yuhr opened this issue May 4, 2021 · 3 comments
Closed

never properties should not require the properties to be present #43954

yuhr opened this issue May 4, 2021 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@yuhr
Copy link

yuhr commented May 4, 2021

Bug Report

πŸ”Ž Search Terms

  • never
  • properties
  • "missing in type"

πŸ•— Version & Regression Information

  • Tested in nightly, 4.2.3, and 3.3.3

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

const x: { foo: never } = {};
// Property 'foo' is missing in type '{}' but required in type '{ foo: never; }'.(2741)

πŸ™ Actual behavior

  • Object literals which are expected to have never properties require initialization for those impossible properties

πŸ™‚ Expected behavior

  • never properties should disallow any initialization

I see there existed a similar issue #16762, but I think this bug is not related to "optional" concept. I believe the current behavior is definitely weird.

@jcalz
Copy link
Contributor

jcalz commented May 5, 2021

Well, it's not a bug... that's how object types and never work. If you have an object of type {bar: string}, then it must have a bar property of type string. If you have an object of type {foo: never}, then it must have a foo property of type never. Since one cannot get one's hands on a value of type never to place into that object, it implies that values of type {foo: never} are similarly elusive. But this is a feature of never, not a bug.

If you want to be able to leave out a property, then you need to make that property optional (here's that "optional" concept). So my suggestion would be to use something like {foo?: never} to mean "an object which cannot have a foo property" instead of {foo: never} which means "an object which must have an impossible foo property".

I can imagine changing this into a suggestion that properties of type never be interpreted the way you want, but then this suggestion would be a breaking change, and I don't know that anyone would think it's worth breaking existing code for it.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label May 5, 2021
@RyanCavanaugh
Copy link
Member

A type with a property of type never has zero legal values; { } is not one of them.

@yuhr
Copy link
Author

yuhr commented May 5, 2021

Since one cannot get one's hands on a value of type never to place into that object, it implies that values of type {foo: never} are similarly elusive. But this is a feature of never, not a bug.

A type with a property of type never has zero legal values; { } is not one of them.

Okay, that sounds. Note that the suggested type { foo?: never } results in { foo?: undefined } which is not the desired type at this point, but it should be eventually resolved by #43947 (I've tested with the insider package and it worked), so once it has been merged successfully, I won't suggest any breaking change about this.

Thank you all! I'd open a suggestion anew in case the PR was declined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants