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

Generated type ActionData, is missing optional properties when fail() returns different data-shapes #9727

Open
jeppech opened this issue Apr 20, 2023 · 2 comments

Comments

@jeppech
Copy link

jeppech commented Apr 20, 2023

Describe the bug

This is only an issue with the generated ActionData-type, not the hydration of the form-property.

When an action is calling fail() multiple places, with different shapes of data, it seems that the generated type will be narrowed down to the shape, that only shares the common properties of all the possible shapes.

This results to Property 'x' does not exist on type 'y'-errors, when trying to access those properties in the +page.svelte component

This seems like a bug. Though not of high severance, as it can be circumvented, by just adding the properties to data-objects where needed.

Related:

Reproduction

https://github.com/jeppech/incorrect-actiondata-multiple-fail-fn

This is a plain create-svelte@latest-repo, that demonstrates how the generated ActionData-type is missing optional properties.

When the data-parameter of fail(), partially matches other shapes, in any other call to the fail-function, the generated type will be narrowed down to the "common" properties of all shapes.

Logs

No response

System Info

❯ pnpx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
.../../.node/pnpm/store/v3/tmp/dlx-55577 |   +1 +
.../../.node/pnpm/store/v3/tmp/dlx-55577 | Progress: resolved 1, reused 1, downloaded 0, added 1, done

  System:
    OS: macOS 13.3.1
    CPU: (8) arm64 Apple M1 Pro
    Memory: 237.09 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.12.1 - ~/Library/pnpm/node
    npm: 8.18.0 - /opt/homebrew/bin/npm
  Browsers:
    Brave Browser: 112.1.50.119
    Firefox Developer Edition: 113.0
    Safari: 16.4
  npmPackages:
    @sveltejs/adapter-auto: ^2.0.0 => 2.0.0
    @sveltejs/kit: ^1.15.7 => 1.15.7
    svelte: ^3.58.0 => 3.58.0
    vite: ^4.2.2 => 4.2.2

Severity

annoyance

Additional Information

No response

@dummdidumm
Copy link
Member

dummdidumm commented Apr 20, 2023

This isn't specific to our typings, it's a weird general TypeScript behavior. If I paste that code into the TS playground I get the same behavior. It seems that TS at some point merges the returned types. It only does this if the object is returned indirectly through a function, not when done directly.

https://www.typescriptlang.org/play?ts=5.0.4#code/GYVwdgxgLglg9mABMAhjANgHgCqIKYAeUeYAJgM6IBKeEcATqZuVPTGAOYA0i4A1mDgB3MAD5RACkQB6aYlJx2HRHTAtEuQsTKUAdPvlw85MAHIoKgBYpOeRDYCeUS0oBQASBYooIcgC5EMBAAWwAjPHouD1JvFADsVwBKeMQAbw96PB96JBioFABuRFcAXwLXV1BIWAREIXoEDglEtNdEGTlfJXtKVXValHR0RDhQgCtaKEoFYzMLCGtbezAnF042xBhgRAkAWW9LXXobBWDmxFFEAAZdAFYW9Pb2zOykVAwJABYrz55Hp-a5BAEAgxn8yEG5DwUQB7QiDXoAQAROwAG6DGCkAD6vgiYBQwTwSJhsNxOQJeGRxBYxI27RKiXK9Iq7S2O32ziOJzgZxalxu91aAJeIByEI+3yufzpTyBILBAVQ6ChJIB8IYyJQILg4CgWLocHQChEtNhKjghuNYACAGYpTKGUzECUWZttnsDlyyDzzvy7g8ZbINC5eihcZRnHYOCQIt48KREABBaDwMAAEViAFooA4AA52KBwRDhQIoegNITxwwiRCFvyBuSpRBy0HkcGhC3oPA2HjqxHN1jdEqIAA+aXw5YYAH4AuBSHhgOwq8Ox0EhjKRWL3ugvlcpeOWwqIcroROEQFTFqamRYljgjA20osbmGrnyKZnYkNi6NmyPZzjm9XkLmuf0hSeINsBDFQwyhCNLCjGNjmIBNk2vDN8mzPMCyLEt8UnSsEytWs4HrAEgybQ82wCDtDW7JAVwPYFW3bTt6OdUdAhAddhSyUU3jQHdJT+ZtmKPJUoU-J0f2ePicnKH8+gsTIgXQKAAHUGk4RAAF46i0ppGVcINYQAPSnCoqhTWo2A4SwoHOf4-w5Q5ANOX1QMFf5ZNeJj5Wo48VTPDVEBRMB0XQTEcShclCWJXgYvxQkqWMKAkWdaTXWcz03J9PlPIDXjfMosSAok08+01bVdX1TsrXig0jWEa1EDtDLvyy90XK9dz8oFQqnk3JASv88Fyt7Sd+0vayb3yO8H3IJ8XzgN8PxdZlfy6nLuWAv0vI3OThtE0bFUhOwyg6jYhoUiolMQFTuKgKgYDsiw9Ns+zmnKEyzIsoA

I created microsoft/TypeScript#53931 in the TS repo.

As a workaround you could try to type the return type shape manually.

@probablykasper
Copy link

Simpler example:

type Result = { error: boolean }

function fun() {
	if (Math.random() > 0.5) {
		return {} as Result & { success: boolean }
	} else {
		return {} as Result
	}
}

fun().success
//    ^ Property 'success' does not exist on type 'Result'.

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

No branches or pull requests

3 participants