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

  • Home
  • SEARCH
  • 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 8757803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:23:26+00:00 2026-06-13T14:23:26+00:00

In JavaScript I can do this: function f() {} f.prop = property; I want

  • 0

In JavaScript I can do this:

function f() {}
f.prop = "property";

I want this in TypeScript, but with type checking.

Other than classes, what TypeScript pattern can I use to enforce that a function gets a property?

Could I use an interface?

interface functionWithProperty {
    (): any;
    prop: string;
}

This seems to be a valid interface in TypeScript, but how do I implement this interface such that the TypeScript compiler checks that prop is set?

I saw this example:

var f : functionWithProperty = (() => {
    var _f : any = function () { };
    _f.prop = "blah";
    return _f;
}());

But this doesn’t work because I can remove _f.prop = "blah"; and everything will still compile. I need to enforce that prop is set.

  • 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-13T14:23:27+00:00Added an answer on June 13, 2026 at 2:23 pm

    I think you need to embrace the object orientation in TypeScript and create a class with properties and functions.

    Combining functions and properties as you have in your example is valid JavaScript, but if you are taking the leap into TypeScript you may as well get fully immersed in it.

    class MyClass {
        constructor(public myProp: string) {
        }
    
        myFunc(): string{
            return this.myProp;
        }
    }
    

    Update

    Disclaimer: I don’t recommend doing things this way – as I’ve said, I recommend using the structural features of TypeScript to organise your code in the most readable way you can.

    However, you can define the type of your function if you want to using a type declaration:

    var f: { (): void; prop: string; } = (() => {
        var _f : any = function () { 
            alert(_f.prop);
        };
        _f.prop = "blah";
        return _f;
    }());
    
    f();
    

    This allows the callers of f to get auto-completion and type checking, but won’t result in the contents of f being checked to ensure it complies – because you are “under the hood” at this stage – so you could write this…

    var f: { (): void; prop: string; } = (() => {
        var _f : any = undefined;
        return _f;
    }());
    
    f();
    

    If you want to get type checking on the definition of f as well as having calls to f checked, you’ll need to look at classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can bind a jquery event to this element like: <script type=text/javascript> $('#new_key').ready(function() {
can someone explain me why this script is not working? <script type=text/javascript> function destroy(ID)
I want to click the video on this page http://37.128.191.200/?640 using JavaScript but can't
In javascript, I can do this: function MyObject(obj) { for (var property in obj)
Can somebody explain to me what this does in javascript? (function (x,y){}(x,y)); or this
Can this be done in JavaScript? type == 1 ? function1() : function2();
Sorry if the title isn't clear enough, but in Javascript you can do this
im new at javascript and i can get this slider to work but not
In Javascript I can do this: function A(x) { return x || 3; }
in my DOM I see this <script type=text/javascript> $(document).ready(function(){ Test.js({Now:\/customers\/product\/1,id:29scds,pro_id:124}); }); </script> i need

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.