1
var a = {
    b:null, 
}

I want to specify that the type of a.b is (num:number) => void while still setting it to null.

Is it possible to do without using class or interface?

1 Answer 1

3

This should work:

let a = {
  b: <(number) => void> null
};

Or you can use a type declaration to make your special function explicit:

declare type MyFun = (number) => void;
let a = {
  b: <MyFun> null
};

Although it's not necessary, I tend to make use of type declarations in my code when there the function has semantics that is not easily caught in the type signature, but can be specified easily in a name.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh. That's basically casting the value into the type you want. It makes sense. Thanks.

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.