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

isObject() asserts untrue types #12253

Closed
tsmaeder opened this issue Mar 2, 2023 · 6 comments · Fixed by #12259
Closed

isObject() asserts untrue types #12253

tsmaeder opened this issue Mar 2, 2023 · 6 comments · Fixed by #12259

Comments

@tsmaeder
Copy link
Contributor

tsmaeder commented Mar 2, 2023

Bug Description:

in core/common/types.ts, we have the following code:

export function isObject<T = Record<string | number | symbol, unknown>>(value: unknown): value is T {
    // eslint-disable-next-line no-null/no-null
    return typeof value === 'object' && value !== null;
}

Let's say we have an interface like so:

interface MyInterface {
   title: string;
}

Now if we do this:

function foo(someObject: unknown) {
   if (isObject<MyInterface>.is(someObject) {
      console.log(someObject.title.toString());
   }
}

foo({});

Note that we don't get a compile problem, since inside the if, someObject is typed as a MyInterface, but we get a runtime error, since the VM frowns upon calling toString() on undefined.

@tsmaeder
Copy link
Contributor Author

tsmaeder commented Mar 2, 2023

@msujew since you brought in this change in #11831, I'd be interested in your thinking.

@tsmaeder
Copy link
Contributor Author

tsmaeder commented Mar 2, 2023

This issue is following up on #12250

@msujew
Copy link
Member

msujew commented Mar 2, 2023

It's imo obvious that the function cannot assert that the input type is actually of type used for the generic parameter. The parameter is there to keep the code more concise and help devs with code completion compared to just object. There might be a comment missing about this though.

@tsmaeder
Copy link
Contributor Author

tsmaeder commented Mar 2, 2023

Hmh...that's sacrificing type safety for convenience. Not something I'm in favor of, in general. Could we assert Partial<T> or something like that? That way, we might get the help from the tooling without asserting non-thruths.

@msujew
Copy link
Member

msujew commented Mar 2, 2023

Yeah, returning Partial<T> probably does make sense.

@paul-marechal
Copy link
Member

paul-marechal commented Mar 4, 2023

Partial<T> is not really better as it's still possible to get in a situation such as:

interface MyType {
    foo: string
}

const unknownValue: unknown = { foo: 2 };

if (isObject<MyType>(unknownValue)) {
    // We made it to this branch, and `unknownValue` now looks like:
    // `{ foo?: string }` but `unknownValue.foo` is currently a number.
}

I opened a PR with the most sane type we might be able to use instead.

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 a pull request may close this issue.

3 participants