方法返回类型无法使用 *此 *

发布于 2025-02-01 14:57:00 字数 1982 浏览 0 评论 0原文

目前,我被分配了一项任务,必须将VUE作为前端框架进行测试,另一个开发人员正在测试React。

主要要点是我们想要强大的类型提示和编译时错误,我正在努力实现方法返回类型。

作品:

<template>
  <div class="home flex flex-col">
    <h1 class="flex justify-center">Spy Table:</h1>
    <h2>Hi: {{ test2() }}</h2>
    <SpyTable />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import SpyTable from "@/components/SpyTable/SpyTable.vue";

function test3(): string {
  return "Vue Test 3";
}

export default defineComponent({
  props: {
    dataset: {
      type: Object as () => FormType,
      required: true,
    },
    name: String,
    msg: { type: String, required: true },
  },
  methods: {
    test1: function (): string {
      return "Vue Test 1";
    },
    test2: function (): typeof test3 {
      return test3;
    },
  },
});
</script>

上面的示例将有效,方法test2暗示返回typeof test3函数,这一切都很好。

不起作用:

<template>
  <div class="home flex flex-col">
    <h1 class="flex justify-center">Spy Table:</h1>
    <h2>Hi: {{ test2() }}</h2>
    <SpyTable />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import SpyTable from "@/components/SpyTable/SpyTable.vue";

function test3(): string {
  return "Vue Test 3";
}

export default defineComponent({
  props: {
    dataset: {
      type: Object as () => FormType,
      required: true,
    },
    name: String,
    msg: { type: String, required: true },
  },
  methods: {
    test1: function (): string {
      return "Vue Test 1";
    },
    test2: function (): typeof this.test1 {
      return this.test1;
    },
  },
});
</script>

这是行不通的,它不会编译,总的来说,只是给我麻烦...我没有办法告诉我test2它应该期望typeof test1要返回?

我真的很想在与Vue合作自己的私人项目时,我真的很喜欢Vue,还有另一个同事正在做与我一样的事,我真的很希望这样公司选择Vue,有人可以帮助我解决这个问题吗?

I'm currently being assigned a task where I have to test out Vue as a frontend framework, another developer is testing out React.

The main takeaway is we want strong type hinting and errors on compile, I'm struggling to implement a method return type.

Works:

<template>
  <div class="home flex flex-col">
    <h1 class="flex justify-center">Spy Table:</h1>
    <h2>Hi: {{ test2() }}</h2>
    <SpyTable />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import SpyTable from "@/components/SpyTable/SpyTable.vue";

function test3(): string {
  return "Vue Test 3";
}

export default defineComponent({
  props: {
    dataset: {
      type: Object as () => FormType,
      required: true,
    },
    name: String,
    msg: { type: String, required: true },
  },
  methods: {
    test1: function (): string {
      return "Vue Test 1";
    },
    test2: function (): typeof test3 {
      return test3;
    },
  },
});
</script>

The above example will work, the method test2 is hinted to return a typeof test3 function, this works all fine.

Does not work:

<template>
  <div class="home flex flex-col">
    <h1 class="flex justify-center">Spy Table:</h1>
    <h2>Hi: {{ test2() }}</h2>
    <SpyTable />
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
import SpyTable from "@/components/SpyTable/SpyTable.vue";

function test3(): string {
  return "Vue Test 3";
}

export default defineComponent({
  props: {
    dataset: {
      type: Object as () => FormType,
      required: true,
    },
    name: String,
    msg: { type: String, required: true },
  },
  methods: {
    test1: function (): string {
      return "Vue Test 1";
    },
    test2: function (): typeof this.test1 {
      return this.test1;
    },
  },
});
</script>

This will not work, it won't compile and in general, just gives me trouble... Is there not a way for me to tell test2 that it should expect a typeof test1 to be returned?

I really would like to be able to do this as I am working with Vue on my own private project, I really like Vue, and there is another colleague who is doing the same as I am just with react, I would really like that the company chooses vue, can someone help me out solving this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文