2017-09-16 77 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 { 
    // ... 
}