-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose stronger typed
SubmitFunction
(#9201)
partially implements #7161 --------- Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
- Loading branch information
1 parent
ae6ddad
commit 369e7d6
Showing
5 changed files
with
115 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/kit': minor | ||
--- | ||
|
||
feat: expose stronger typed `SubmitFunction` through `./$types` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/kit/src/core/sync/write_types/test/actions/+page.server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { fail } from '../../../../../../types/internal.js'; | ||
|
||
let condition = false; | ||
|
||
export const actions = { | ||
default: () => { | ||
if (condition) { | ||
return fail(400, { | ||
fail: 'oops' | ||
}); | ||
} | ||
|
||
return { | ||
success: true | ||
}; | ||
}, | ||
successWithPayload: () => { | ||
return { | ||
id: 42, | ||
username: 'John Doe', | ||
profession: 'Svelte specialist' | ||
}; | ||
}, | ||
successWithoutPayload: () => {}, | ||
failWithPayload: () => { | ||
return fail(400, { | ||
reason: { | ||
error: { | ||
code: /** @type {const} */ ('VALIDATION_FAILED') | ||
} | ||
} | ||
}); | ||
}, | ||
failWithoutPayload: () => { | ||
return fail(400); | ||
} | ||
}; | ||
|
||
/** @type {import('./.svelte-kit/types/src/core/sync/write_types/test/actions/$types').SubmitFunction} */ | ||
const submit = () => { | ||
return ({ result }) => { | ||
if (result.type === 'success') { | ||
// @ts-expect-error does only exist on `failure` result | ||
result.data?.fail; | ||
// @ts-expect-error unknown property | ||
result.data?.something; | ||
|
||
if (result.data && 'success' in result.data) { | ||
result.data.success === true; | ||
// @ts-expect-error should be of type `boolean` | ||
result.data.success === 'success'; | ||
// @ts-expect-error does not exist in this branch | ||
result.data.id; | ||
} | ||
|
||
if (result.data && 'id' in result.data) { | ||
result.data.id === 42; | ||
// @ts-expect-error should be of type `number` | ||
result.data.id === 'John'; | ||
// @ts-expect-error does not exist in this branch | ||
result.data.success; | ||
} | ||
} | ||
|
||
if (result.type === 'failure') { | ||
result.data; | ||
// @ts-expect-error does only exist on `success` result | ||
result.data.success; | ||
// @ts-expect-error unknown property | ||
result.data.unknown; | ||
|
||
if (result.data && 'fail' in result.data) { | ||
result.data.fail === ''; | ||
// @ts-expect-error does not exist in this branch | ||
result.data.reason; | ||
} | ||
|
||
if (result.data && 'reason' in result.data) { | ||
result.data.reason.error.code === 'VALIDATION_FAILED'; | ||
// @ts-expect-error should be a const | ||
result.data.reason.error.code === ''; | ||
// @ts-expect-error does not exist in this branch | ||
result.data.fail; | ||
} | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters