首先安装 readline-sync 和 @types/readline-sync,如下:
pnpm install readline-sync
pnpm install @types/readline-sync -D
具体实现如下:
// readline.ts
import readlineSync from 'readline-sync'
export default function readline() {
return readlineSync.prompt()
}
使用案例:
import readline from './src/readline.ts'
const n = parseInt(readline())
const arr = readline()!.split(' ').map(Number)
console.log(n, arr)
_
ts
模板可以使用下面的方式加载,免去配置环节,degit
需要在全局安装一下使用npm。
degit github:hhk-png/ts-template readline
参考资料
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/yield