You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Vue Props添加带自动补全的HTML属性Type类型?

无需自定义枚举的解决方案

你可以直接使用TypeScript内置的HTMLInputTypeAttribute类型,它已经预定义了所有合法的HTML输入框type属性值,能提供完整的类型提示和校验,无需手动枚举:

import Input from "./input/Input.vue";

interface SearchInputProps {
  search: string;
  placeholder?: string;
  type?: HTMLInputTypeAttribute; // 自动提示所有合法输入类型
}

withDefaults(defineProps<SearchInputProps>(), {
  placeholder: "Search",
  type: "text",
});

const emit = defineEmits<{
  (event: "search-input", input: string): void;
}>();
</script>

这个类型是TypeScript专门为input元素的type属性设计的,涵盖了所有标准值(比如text、password、email、number等),同时也兼容部分浏览器支持的非标准值,完全满足需求。

内容的提问来源于stack exchange,提问作者Just V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:22:34