最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reference an inferred type in TypeScript - Stack Overflow

programmeradmin1浏览0评论

Is there any way to reference an inferred type in TypeScript?

In the following example we get nice inferred types.

function Test() {
    return {hello:"world"}
}

var test = Test()
test.hello // works
test.bob   // 'bob' doesn't exist on inferred type

But what if I want to define a function that takes a parameter of the type: "Whatever Test returns", without explicitly defining the interface?

function Thing(test:???) {
  test.hello // works
  test.bob   // I want this to fail
}

This is a workaround, but it gets hairy if Test has parameters of its own.

function Thing(test = Test()) {} // thanks default parameter!

Is there some way to reference the inferred type of whatever Test returns? So I can type something as "Whatever Test returns", without making an interface?

The reason I care is because I usually use a closure/module pattern instead of classes. Typescript already lets you type something as a class, even though you can make an interface that describes that class. I want to type something as whatever a function returns instead of a class. See Closures in Typescript (Dependency Injection) for more information on why.

The BEST way to solve this is if TypeScript added the abilty to define modules that take their dependencies as parameters, or to define a module inside a closure. Then I could just use the spiffy export syntax. Anyone know if there are any plans for this?

Is there any way to reference an inferred type in TypeScript?

In the following example we get nice inferred types.

function Test() {
    return {hello:"world"}
}

var test = Test()
test.hello // works
test.bob   // 'bob' doesn't exist on inferred type

But what if I want to define a function that takes a parameter of the type: "Whatever Test returns", without explicitly defining the interface?

function Thing(test:???) {
  test.hello // works
  test.bob   // I want this to fail
}

This is a workaround, but it gets hairy if Test has parameters of its own.

function Thing(test = Test()) {} // thanks default parameter!

Is there some way to reference the inferred type of whatever Test returns? So I can type something as "Whatever Test returns", without making an interface?

The reason I care is because I usually use a closure/module pattern instead of classes. Typescript already lets you type something as a class, even though you can make an interface that describes that class. I want to type something as whatever a function returns instead of a class. See Closures in Typescript (Dependency Injection) for more information on why.

The BEST way to solve this is if TypeScript added the abilty to define modules that take their dependencies as parameters, or to define a module inside a closure. Then I could just use the spiffy export syntax. Anyone know if there are any plans for this?

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Dec 7, 2012 at 19:22 Sean Clark HessSean Clark Hess 16.1k13 gold badges54 silver badges102 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It's now possible:

function Test() {
    return { hello: "world" }
}

function Thing(test: ReturnType<typeof Test>) {
  test.hello // works
  test.bob   // fails
}

https://www.typescriptlang/docs/handbook/advanced-types.html#type-inference-in-conditional-types

You can use the body of an interface as a type literal:

function Thing(test: { hello: string; }) {
    test.hello // works
    test.bob   // I want this to fail
}

is equivalent to

interface ITest {
    hello: string;
}

function Thing(test: ITest) {
    test.hello // works
    test.bob   // I want this to fail
}

Just don't forget the ; at the end of each member.


There is no syntax for naming or referring to inferred types. The closest you can get is using interfaces or type-literals for the members you are going to use. Interfaces and type-literals will match whatever type which at least has the defined members. "Duck-typing"

发布评论

评论列表(0)

  1. 暂无评论