VSC @TypeScript Observable错误

时间:2017-09-16 03:01:01

标签: typescript

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'.'"

任何线索我在这里做错了什么?我认为这是我的语法,但我不确定

1 个答案:

答案 0 :(得分:0)

promptString的定义是:

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

但是在您的代码中,您传递的第二个参数cbforkB,其中键入为(choice: number): void - 其中numberstring不兼容。

将您的forkB更改为:

export function forkB(choice: string): void {
    // ...
}