void as a type of an argument of a generic function in TypeScript

前端 未结 7 1251
遥遥无期
遥遥无期 2021-01-11 09:37

It is possible to specify void as a type parameter for generic function in TypeScript. And it works fine for return values of functions. However when void

相关标签:
7条回答
  • 2021-01-11 09:46

    Like @c-snover said, void serves almost no useful purpose but it is allowed as function parameter type or as type argument for a generic type T, so it cannot be removed from the spec.

    TLDR ⇒ Declaring a function(v: void) is like saying v is not there. You can though return d, which is equivalent to just return, but you can't pass any value of any type to that function argument (well, except a variable of type void|undefined and value undefined).

    In the case of generic type arguments, on instantiation, the same rules apply. If T is passed void on instantiation, whereever the type T is used in that class, it's substituted by void, so the above applies again.


    Functionally, void is the same as undefined, except in some cases where it is implicit, like in function returns when there's nothing being returned.

    undefined is used implicitly only with optional function parameters (eg. function(variable?: number) where variable's type effectively becomes number|undefined. If the parameter doesn't get passed any value on a function call, it is implicitly undefined.

    In contrast a declaration of function(variable: number|undefined) would mean that the parameter is now mandatory and it needs to be passed a value explicitly, including explicitly passing undefined, if that's what you want.

    0 讨论(0)
  • 2021-01-11 09:47

    You can cast undefined to void:

    const x: void = undefined as void
    
    0 讨论(0)
  • 2021-01-11 09:55

    It turned out it's very easy to get a value of void. Although technically it is still the undefined value, but TypeScript believes it's of the type void which is exactly what we need:

    var voidValue = (function () { })();
    
    0 讨论(0)
  • 2021-01-11 09:58

    From the TypeScript specification:

    NOTE: We might consider disallowing declaring variables of type Void as they serve no useful purpose. However, because Void is permitted as a type argument to a generic type or function it is not feasible to disallow Void properties or parameters.

    0 讨论(0)
  • 2021-01-11 10:00

    A useful use for void is when you want to make a parameter optional. Angular uses this a lot with RxJS Subject<T> when emitting events and there's no useful type for the event.

    The signature for Subject<T>'s next function is :

    next(value?: T)

    You can create a Subject<void> which makes the value parameter forbidden:

    new Subject<void>().next() // ok

    new Subject<void>().next(true) // not allowed

    as opposed to non-void where you get

    new Subject<boolean>().next(true)

    Not sure exactly which TS version this became possible.

    0 讨论(0)
  • 2021-01-11 10:03

    You can do something like this:

    class Example<T> {
        process(func: T = undefined) {
        // use default undefined parameter
        }
    }
    
    var example = new Example<void>();
    
    exmple.process();
    

    I'm using Typescript 1.8.7 and it worked for me fine.

    0 讨论(0)
提交回复
热议问题