Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Typescript types do not work on undefined as first parameter #67

Closed
macalinao opened this issue Dec 6, 2018 · 5 comments
Closed

Typescript types do not work on undefined as first parameter #67

macalinao opened this issue Dec 6, 2018 · 5 comments

Comments

@macalinao
Copy link

The following does not work:

          const myVar: { a: { b: string } } | null = null as {
            a: { b: string };
          } | null;
          const something = idx(myVar, _ => _.a.b);
@Horaddrim
Copy link

Could you provide more details about how exactly it should work? Your issue doesn't say anything 😅 It doesn't work in the transpile fase? Or at the runtime?

@macalinao
Copy link
Author

This should have no compile errors, but it does.

The error is [ts] Object is possibly 'null'. [2531].

@Horaddrim
Copy link

If you are using the { strict: true } option in your tsconfig.json, it will always check about possible null values, and for never values too, so, if you want to compile your code, you can disable that, or do a check in this variable, because the idx type definition returns a possible null in this line :D
Checking the return here const something = idx(myVar, _ => _.a.b); with something like -> const something = idx(myVar, _ => _.a.b) || 'Error message here'; will solve the problem.

@yungsters
Copy link
Contributor

Thanks, @Horaddrim.

Sounds like this question has been resolved.

@macalinao
Copy link
Author

macalinao commented Dec 24, 2018

@Horaddrim This doesn't solve the problem for me.

    const myVar: { a: { b: string } } | null = null as {
      a: { b: string };
    } | null;
    const something = idx(myVar, _ => _.a.b) || "TEST";

still fails the build.

Please let me know if I failed any etiquette by opening a new issue. I'm not sure if I was supposed to do that-- sorry if I'm being annoying!

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

No branches or pull requests

3 participants