Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:40:57+00:00 2026-06-15T14:40:57+00:00

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

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:40:58+00:00Added an answer on June 15, 2026 at 2:40 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way I can reference parent value, like per example .btn-blue {
Is there any way to speed up the Add Reference dialog? For example, has
Given an instance of an unknown reference or value type, is there any way
Is there any way to implement a type of reference whose value can be
Is there any way in Java to get the compile time type of a
Is there any way possible short of hand-coding the Reference.cs file, to get Visual
is there any way to reference a class name from within the class declaration?
Is there any way to add reference from one winforms to another winforms?? There
Is there any way to make a DataTemplate reference itself just from XAML? In
Is there any way to indicate to ReSharper that a null reference won't occur

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.