将通用参数限制为类型而不是类型的实例

时间:2019-11-30 23:40:04

标签: typescript generics types

是否可以使用参数作为类型而不是类型的实例来指定泛型?

我有一个第三部分模块,要求我传递Type(而不是实例),但我希望能够将我的签名参数限制在接口上。

对于(不良)示例:

import random

score = 0
health = 75
maxhealth = 100
keys = 0
health_potions = 0
skip_stage = 1
cmd = ""
inc = 0

def hiahelp():
    #code
def stats():
    #code
def oCmds():
    global cmd
    global inc
    global health
    if cmd == "stats":
        stats()
        inc = 0
    elif cmd == "help":
        hiahelp()
        inc = 0
    elif cmd == "use health potion":
        #healing code
    elif cmd == "use":
        print("incomplete command")
    else:
        inc = 1

#intro code

while health > 0:

#more intro code

    print("What is HungryIronApple's favorite music artist (besides himself, include proper capitalization)?")
    def stage1():
        r = True
        while r:
            global inc
            global cmd
            global health
            if inc == 1:
                print("incorrect, try again")
                health = health - 10
            cmd = input("answer: ")
            if cmd == "Alan Walker":
                inc = 0
                r = False
            elif cmd == "left" or cmd == "right":
                print("originally I would take health away for such a dumb mistake but because we are just getting started, I'll ignore it")
            else:
                oCmds()
    stage1()
    print("the door opens")
    quit()
print(f"you died {username}")

class Foo {} class Bar{ // obviously I can't use typeOf here... static method<T extends MyInterface>(fooType: typeOf(T)) { // signature: thirdPartyMethod(component: any); return thirdPartyMethod(fooType); } } interface MyInterface { } // usage: const foo1 = Bar.method<Foo>(Foo); // or I'd assume with the answer, Typescript should imply it: const foo2 = Bar.method(Foo); // thirdPartyMethod usage: const foo3 = thirdPartyMethod(Foo); // actually instantiates Foo and returns it. foo1 instanceof Foo // true foo2 instanceof Foo // true foo3 instanceof Foo // true 的签名应该是什么样(如果可能的话)?

0 个答案:

没有答案