2017-09-16 45 views
3
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阶段的代码,我得到以下错误:VSC @TypeScript可观察到的错误

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

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

 

回答

0

promptString的定义是:

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

但在你的代码要传递的第二个参数作为cb其中forkB键入作为(choice: number): void - 其中numberstring不兼容。

更改forkB为:

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