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

Types not inferred correctly for Arrays with additional properties anymore #3677

Open
doepnern opened this issue Nov 15, 2024 · 3 comments
Open
Labels

Comments

@doepnern
Copy link

doepnern commented Nov 15, 2024

What version of Hono are you using?

6.6.10

What runtime/platform is your app running on? (with version if possible)

Browser

What steps can reproduce the bug?

import type { JSONParsed } from 'hono/utils/types';

type TestWorking = { foo: 'bar' }[];
type TestBroken = { foo: 'bar' }[] & { meta: 'v1' };
type ResultWorking = JSONParsed<TestWorking>;
type ResultBroken = JSONParsed<TestBroken>;

What is the expected behavior?

type ResultWorking = {
    foo: "bar";
}[]

What do you see instead?

type ResultBroken = {
    [x: number]: {
        foo: "bar";
    };
    length: number;
    toString: null;
    toLocaleString: null;
    pop: null;
    push: {};
    concat: {};
    join: {};
    reverse: null;
    shift: null;
    slice: {};
    sort: {};
    splice: {};
    unshift: {};
    indexOf: {};
    ... 27 more ...;
    meta: "v1";
}

Additional information

We are using postgres.js and sometimes want to return a RowList, which is an Array, extended with some additional properties.
When returning an Array with additional properties in hono, it will not be resolved as a simple Array (even though the JSON transformation will remove the additional properties).

So in this case, the frontend code will expect the returned Response to have a property meta, even though it will be stripped in transport. This was working as expected in an earlier version. See https://github.com/honojs/hono/blob/2d0135956c96e00fee48fb5faf287dbc9daf7f4f/src/utils/types.ts

@yusukebe
Copy link
Member

yusukebe commented Nov 15, 2024

Hi @doepnern

Sorry, I can't fully understand your problem. If the following:

type T = JSONParsed<TestBroken>

What do you expect the type of T?

And I think JSON string can't have { foo: 'bar' }[] & { meta: 'v1' } structure. Do you mean it should be { foo: 'bar' }[]?

@yusukebe
Copy link
Member

yusukebe commented Nov 15, 2024

@doepnern

This means if you have the following object and you do JSON.stringify, the value will be [1, 2, 3], which means the type should also be number[], right?

const data = Object.assign([1, 2, 3], { foo: 'bar' })
console.log(JSON.stringify(data)) // [1,2,3] => number[]

@m-shaka I'll investigate it later but, can you take a look at this?

@doepnern
Copy link
Author

@yusukebe Yeah, I would expect it to be the same as the the one without the additional properties, a simple Array. As long as your code (and the typescript) detects the type to be an array the output will loose all additional properties anyway when being stringified.

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

No branches or pull requests

2 participants