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

Wrong TypeScript type when using encore:"optional" without omitempty #1623

Open
melkstam opened this issue Dec 3, 2024 · 0 comments
Open

Comments

@melkstam
Copy link
Contributor

melkstam commented Dec 3, 2024

When using the encore:"optional" and generating a client, the field is marked as field?: type. However, this typing is only correct when omitempty is included in the json tag.

Sometimes you actually don't want to add an omitempty, for example if you want to convey that there is a field here, but it just has no value.


For example, take this simple endpoint:

package hello

import (
	"context"
)

type HelloResponse struct {
	Foo *string `json:"foo" encore:"optional"`
	Bar *string `json:"bar,omitempty" encore:"optional"`
}

// encore:api public
func Hello(ctx context.Context) (*HelloResponse, error) {
	return &HelloResponse{
		Foo: nil,
		Bar: nil,
	}, nil
}

When generating a TypeScript client, the type of HelloResponse is

export interface HelloResponse {
    foo?: string
    bar?: string
}

However, when actually calling the endpoint and parsing it in JavaScript, the response you get is

{
    "foo": null
}

which actually matches to the type

export interface HelloResponse {
    foo: string | null
    bar?: string
}
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

No branches or pull requests

1 participant