1

I am looking to find a way to make the following piece of code compile without having to declare an interface.

var a : <A>{ (value: A): void; (): A; } = null; 
6
  • Why can't you create a named interface? Is the operation so abstract you can't even name it? Commented Oct 3, 2014 at 15:50
  • 1
    I don't want to pollute the namespace with single-time-used interfaces. Commented Oct 3, 2014 at 15:52
  • I haven't used TS in a while, but can't you do something like a local, "detail" namespace? Commented Oct 3, 2014 at 15:53
  • 1
    Of course I can come up with a workaround, but the question is about finding a way to avoid doing that. Commented Oct 3, 2014 at 15:54
  • 1
    TypeScript claims to support anonymous types, hence the question. Commented Oct 3, 2014 at 15:56

1 Answer 1

1

It doesn't make sense for a generic type like that to even exist. It's the same reason you can't write Foo<T> x; in C++ without an actual T in scope -- once the type is manifest, it has to be bound.

You can write this instead, which probably has the semantics you wanted anyway.

var a: { <A>(value: A): void; <A>(): A; } = null; 

If that's not what you were looking for, it'd be useful to post some examples of what valid and invalid calls on a would look like.

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

1 Comment

It turned out that my case was { <A>(value: A): void; <B>(): B; }

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.