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

How to shorten: "prop" in obj && obj.prop #173

Open
devinrhode2 opened this issue Oct 11, 2022 · 0 comments
Open

How to shorten: "prop" in obj && obj.prop #173

devinrhode2 opened this issue Oct 11, 2022 · 0 comments

Comments

@devinrhode2
Copy link

devinrhode2 commented Oct 11, 2022

Optional chaining solved this in the javascript world, but typescript just doesn't like this looser js-style code.

I wish I could just write:

obj?.prop

Instead of:

"prop" in obj && obj.prop

when obj is:

type Obj = { prop: string } | { error: string, code: number }

Maybe something like a lodash.get could solve this, using some typescript magic.

Related twitter thread: https://twitter.com/mattpocockuk/status/1579834794472771584

@devinrhode2 devinrhode2 changed the title How to shorten "prop" in obj && obj.prop How to shorten ""prop" in obj&&obj.prop" Oct 11, 2022
@devinrhode2 devinrhode2 changed the title How to shorten ""prop" in obj&&obj.prop" How to shorten ""prop" in obj && obj.prop" Oct 11, 2022
@devinrhode2 devinrhode2 changed the title How to shorten ""prop" in obj && obj.prop" How to shorten \`"prop" in obj && obj.prop\` Oct 11, 2022
@devinrhode2 devinrhode2 changed the title How to shorten \`"prop" in obj && obj.prop\` How to shorten "prop" in obj && obj.prop Oct 11, 2022
@devinrhode2 devinrhode2 changed the title How to shorten "prop" in obj && obj.prop How to shorten: "prop" in obj && obj.prop Oct 11, 2022
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

1 participant