用于推断接口实现类中的类型参数的 TypeScript [英] TypeScript to infer type arguments in interface implementation class

查看:29
本文介绍了用于推断接口实现类中的类型参数的 TypeScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它会抱怨 arg 的类型是 any

It will complain that type of arg is any

interface Foo { 
  foo(arg: string): void 
}

class FooImpl implements Foo {
  foo(arg) { console.log(arg) }
}

我当然可以指定它是 string 但它是重复的.

I can of course specify that it's string but it's repetitive.

也许有一种方法可以调整 TypeScript 使其自动推断它是 string?

Maybe there's a way to tune TypeScript to make it infer that it's string automatically?

推荐答案

也许有一种方法可以调整 TypeScript 以使其自动推断它是字符串

Maybe there's a way to tune TypeScript to make it infer that it's string automatically

没有.这是相关的 TypeScript 问题及其不存在的原因:https://github.com/Microsoft/打字稿/问题/1373

No. Here is the relevant TypeScript issue with reasons why its not there: https://github.com/Microsoft/TypeScript/issues/1373

这篇关于用于推断接口实现类中的类型参数的 TypeScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆