import "introcs";

export function main(): void {
    print ("Intro");
    print ("a");
    print ("b");
    promptString("What would like to do?", forkMain);
}

export function forkMain(choice: string): void {
    clear();
    if ("a") {
        storyA();
    } else if ("b") {
        storyB();
    } else {
        main();
    }
}

export function storyA(): void {
    print ("result");
    print ("1");
    print ("2");
    promptNumber("What would like to do?", forkA);
}

export function forkA(choice: number): void {
    clear();
    if (1) {
        storyC();
    } else if (2) {
        storyD();
    } else {
        storyA();
    }
 }

export function storyB(): void {
    print ("result");
    print ("3");
    print ("4");
    promptString("What would like to do?", forkB);
}

export function forkB(choice: number): void {
    clear();
    if (1) {
        storyE();
    } else if (2) {
        storyF();
    } else {
        storyB();
    }
 }

export function storyC(): void {
    print ("the end story c");
}

export function storyD(): void {
    print ("the end story d");
}

export function storyE(): void {
    print ("the end story e");
}

export function storyF(): void {
    print ("the end story f");
}

main();

嘿,伙计们,上面是我正在为cyoa工作的开始阶段的代码,但是我在打印阶段遇到了问题,因为一旦它到达代码的forkb阶段,我会得到以下错误:
"severity: 'Error'
message: 'Argument of type '(choice: number) => void' is not assignable to parameter of type '(value: string) => void'.
  Types of parameters 'choice' and 'value' are incompatible.
    Type 'string' is not assignable to type 'number'.'"

你知道我做错什么了吗?我想这是我的语法,但我不确定
_

最佳答案

promptString的定义是:

function promptString(prompt: string, cb: (value: string) => void): void;

但是在您的代码中,您将第二个参数cb作为forkB传递,其类型为(choice: number): void--其中numberstring不兼容。
forkB更改为:
export function forkB(choice: string): void {
    // ...
}

关于typescript - VSC @TypeScript可观察的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46249627/

10-12 00:25