0

I thought using the type Object would do it, but that still allows anything. I want something like this:

function foo(arg: /* what goes here? */) { }

foo({})                        // should compile
foo({ something: 'anything'})  // should compile
foo(new Object())              // should compile

foo(7)                         // should not compile
foo('hello')                   // should not compile

1 Answer 1

2

There is no way to set Javascript Object type constraint in the compile time in TypeScript 1.4. But you can still check the type in the run-time.

function foo(arg: Object) {
    if (typeof arg !== "object") { throw new Error("Error"); }

    console.log(arg);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm actually writing a definition file for javascript that does something like that so, a shame it can't be done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.