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

Realm.List subscript accessor ([]) has incorrect return type #4750

Closed
myou11 opened this issue Aug 2, 2022 · 5 comments
Closed

Realm.List subscript accessor ([]) has incorrect return type #4750

myou11 opened this issue Aug 2, 2022 · 5 comments
Assignees

Comments

@myou11
Copy link

myou11 commented Aug 2, 2022

How frequently does the bug occur?

All the time

Description

The return type for subscript access on a Realm.List seems to be incorrect. In the index.d.ts from the realm package, the definition of the subscript access is:

interface List<T> extends Collection<T> {
    [n: number]: T;
    .....

I think the return type should be T | undefined since specifying an n that is out of bounds does produce an undefined result.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

No response

Version

10.20.0-beta.5

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

iOS 15

Build environment

Which debugger for React Native: ..
Hermes

Cocoapods version

1.11.2

@kneth
Copy link
Member

kneth commented Aug 3, 2022

@myou11 Thank you for reporting. As both Realm.List and Realm.Results mimics Array, undefined is returned if you are out of bound. The TypeScript definition needs to be fixed.

@kneth kneth self-assigned this Aug 3, 2022
@kneth
Copy link
Member

kneth commented Aug 3, 2022

After studying it a bit more careful, I think the type is correct. From the discussion, it is on purpose that ReadonlyArray doesn't have T | undefined as return type for [].

@myou11
Copy link
Author

myou11 commented Aug 3, 2022

Thanks for looking into this @kneth! I appreciate the linking to that Typescript discussion. After reading it, I understand why they have the return type as just T rather than T | undefined.
I do see that they have a tsconfig option noUncheckedIndexedAccess that provides the | undefined in the index signatures. Is there a way to get something like this for Realm.List and Realm.Results?

@kneth
Copy link
Member

kneth commented Aug 21, 2022

Is there a way to get something like this for Realm.List and Realm.Results?

Just to be clear, you only want it for Realm.List and Realm.Results? I mean, noUncheckedIndexedAccess affects all arrays.

The rationale behind our current types is to promote that you guard against out-of-range situations. It means, accessing an element should never return undefined.

@myou11
Copy link
Author

myou11 commented Aug 22, 2022

After re-visiting this, the types provided are good! For my use case before, I was already having checks to guard against out-of-range situations. I realize enabling the noUncheckedIndexedAccess would be a bit too strict for all our other uses similar to what is discussed in the discussion you linked.

You can disregard this issue. Thanks for the discussion!

@myou11 myou11 closed this as completed Aug 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants