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

Small TS strict improvements in trackHistory and batching #56

Merged
merged 2 commits into from
Oct 29, 2022

Conversation

Manc
Copy link
Contributor

@Manc Manc commented Oct 29, 2022

In trackHistory an object type was defined as Record<number, Partial<T>>. The keys are timestamps.

However, a test failed (in strict mode) due to a typing error when the key was expected to be a number, but Object.keys() actually returns strings. Object keys are implicitly cast to stings even when set as numbers, so the keys will always be strings.

Unrelated but tiny change: I also added a type for let timeout.

@jmeistrich
Copy link
Contributor

I would prefer to keep the timestamps as numbers because they are smaller in size - a 13 digit timestamp is 8 bytes as a number vs. 26 bytes as a string. It's especially important when persisting the timestamps to a database. Can we improve the types while keeping it as a number?

@Manc
Copy link
Contributor Author

Manc commented Oct 29, 2022

The problem here is that number keys on a JS object do not exist. If you set a property with a number key, JS will automatically set it with a string key. See here. This is why Object.keys() always returns an Array<string>. So we may as well set the TS key type to string. See also here.

const obj = { 123: true };
obj // { '123': true }

@jmeistrich
Copy link
Contributor

Huh, 10 years into JS development and I never knew that 😅

@jmeistrich jmeistrich merged commit 875e235 into LegendApp:main Oct 29, 2022
@Manc
Copy link
Contributor Author

Manc commented Oct 29, 2022

I know I know, I still find myself learning by simply using TS. I didn’t even look what this history is used for, but if you expect a lot of entries, maybe a tuple array may be more suitable here, like Array<[number, Entry]>.

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 this pull request may close these issues.

None yet

2 participants