为已解决的Promise类型问题(Svelte/TypeScript)

huangapple go评论70阅读模式
英文:

Type issue for a resolved promise (Svelte/TypeScript)

问题

我有以下的简化的Typescript中的Svelte代码:

<script lang="ts">
	import axios from 'axios';
	import { env } from "$env/dynamic/public";
	import type { Submission } from '../models/submission';

	let submissionUuid: string | undefined;
	let submissionStatusResponse = Promise.resolve(String);

	const postSubmission = async (file: File): Promise<Partial<Submission>> => {
		const formData = { file: file };
		const { data, status } = await axios.post(env.PUBLIC_API_BASE_URL + '/submissions/', formData, {
			headers: { 'Content-Type': 'multipart/form-data' }
		});
		if (status === 200) {
			// console.log(JSON.stringify(data));
			return data;
		}
	};

	const getSubmissionStatus = async (uuid: string) => {
		const { data, status } = await axios.get(env.PUBLIC_API_BASE_URL + '/submissions/' + uuid);
		if (status === 200) {
			// console.log(JSON.stringify(data));
			return data.status;
		}
	};

	function handleSubmit() {
		let submissionResponse: Promise<Partial<Submission>> = postSubmission(
			document.getElementById('file').files[0]
		);
		submissionResponse.then((submission) => {
			submissionUuid = submission.uuid;
			submissionStatusResponse = getSubmissionStatus(submissionUuid);
		});
	}
</script>

<form on:submit|preventDefault={handleSubmit}>
	<label for="file">Select a file</label>
	<input type="file" id="file" name="file" required />
	<button type="submit">Submit</button>

	{#await submissionStatusResponse then submissionStatus}
		{submissionStatus}
	{:catch error}
		Error getting submission status from API: {error.message}
	{/await}
</form>

但在网页上,它呈现了字符串 function String() { [native code] },而不是 submissionStatus 的解析值。这肯定是一个类型问题,因为在组件 Status 上传递 假定已解析submissionStatus 字符串时,会引发以下Typescript警告:Type 'StringConstructor' is not assignable to type 'string'

我在这里做错了什么?

英文:

I have the following simplified Svelte code in Typescript:

<script lang="ts">
	import axios from 'axios';
	import { env } from "$env/dynamic/public";
	import type { Submission } from '../models/submission';

	let submissionUuid: string | undefined;
	let submissionStatusResponse = Promise.resolve(String);
    // let submissionStatusResponse = Promise<string>; // not sure about the difference

	const postSubmission = async (file: File): Promise<Partial<Submission>> => {
		const formData = { file: file };
		const { data, status } = await axios.post(env.PUBLIC_API_BASE_URL + '/submissions/', formData, {
			headers: { 'Content-Type': 'multipart/form-data' }
		});
		if (status === 200) {
			// console.log(JSON.stringify(data));
			return data;
		}
	};

	const getSubmissionStatus = async (uuid: string) => {
		const { data, status } = await axios.get(env.PUBLIC_API_BASE_URL + '/submissions/' + uuid);
		if (status === 200) {
			// console.log(JSON.stringify(data));
			return data.status;
		}
	};

	function handleSubmit() {
		let submissionResponse: Promise<Partial<Submission>> = postSubmission(
			document.getElementById('file').files[0]
		);
		submissionResponse.then((submission) => {
			submissionUuid = submission.uuid;
			submissionStatusResponse = getSubmissionStatus(submissionUuid);
		});
	}
</script>

<form on:submit|preventDefault={handleSubmit}>
	<label for="file">Select a file</label>
	<input type="file" id="file" name="file" required />
	<button type="submit">Submit</button>

	{#await submissionStatusResponse then submissionStatus}
		{submissionStatus}
	{:catch error}
		Error getting submission status from API: {error.message}
	{/await}
</form>

But in the web page the it renders the string function String() { [native code] } instead of the resolved value of submissionStatus.

There must be indeed a type issue since the following Typescript alert is raised on the component Status, when passing the supposedly resolved submissionStatus string: Type 'StringConstructor' is not assignable to type 'string'.

What am I doing wrong here?

答案1

得分: 1

问题在于你正在赋值给submissionStatusResponse,而不是进行注解:

let submissionStatusResponse = Promise.resolve(String);

因此,Promise可以立即解析String。结果是function String() { [native code] }(尝试使用console.log(String)进行比较)。

我认为你想要的是:

let submissionStatusResponse: Promise<string>; // 一个将返回字符串的 promise

然后,一旦 promise 被解析,将返回一个字符串。

英文:

The problem is that you are assigning submissionStatusResponse rather than annotating it:

let submissionStatusResponse = Promise.resolve(String);

So then Promise can resolve String right away. The result is function String() { [native code] } (try doing console.log(String) for comparison).

I think you want:

let submissionStatusResponse: Promise&lt;string&gt;; // A promise that will return a string

Then, once the promise is resolved, a string will be returned.

huangapple
  • 本文由 发表于 2023年7月13日 00:54:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定