Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

generics - Typescript equivalent of ? unknown wildcard

Say I want to declare an array of generics in Typescript. What would be the equivalent of the unknown wildcard '?' in Java?

This does compile, but seems a little awkward MyGeneric<any>[];

I would like a declaration equivalent to Java MyGeneric<?>[];, that is, a single type array, but the type is not known when it is declared.

Cheers.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think Typescript has late binding generic types.

But if this particular code was in the context of a function you could use generics to achieve a similar thing.

function example<T>(val:T){
  let a : T[]

  return [val]
}

let a = example(4)
// number[] :: [4]

let b = example('hello')
// string[] :: ['hello']

Here is an interactive example: link

Try hovering over a or b to see it in action.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...