英文:
How to create an array of strongly typed arrays?
问题
I tried to write such simple code:
let arr: Array<[string, number]> = ['dad\'s adidas', 123], ['moms gucci', 47];
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
console.log(arr[i][j]);
}
console.log('\n');
}
但编译器报错并显示以下错误:
Uncaught SyntaxError C:\Users\sv25b\UbuntuProjects\WTF\main.ts:1
let arr: Array<[string, number]> = ['dad\'s adidas', 123], ['moms gucci', 47];
我做错了什么?
英文:
I'm new in TS, but i didn't find answer for my question, so that's why I'm here)
I'm trying to create an array of arrays, that may contain only strongly typed data. For example like [[string, number], [string, number], [string, number], ...]
I tried to write such simple code:
let arr: Array<[string, number]> = [['dad\'s adidas', 123], ['moms gucci', 47]]
for (let i = 0; i < arr.length; i++) {
for (let j = 0; i < arr[i].length; j++) {
console.log(arr[i][j])
}
console.log('\n')
}
but the The compiler swears and give me this stupid error))
Uncaught SyntaxError C:\Users\sv25b\UbuntuProjects\WTF\main.ts:1
let arr: Array<[string, number]> = [['dad\'s adidas', 123], ['moms gucci', 47]]
What did I do wrong?
答案1
得分: 0
- 运行
npm install tsx
- 安装 typescript runner- 或者全局安装它,使用
npm install --global tsx
- 或者全局安装它,使用
- 运行
npx tsx watch main.ts
以在监视模式下运行您的文件(在文件保存时重新启动)- 或者如果您已全局安装了 tsx,运行
tsx watch main.ts
- 或者使用 https://marketplace.visualstudio.com/items?itemName=rxliuli.tsx
- 或者使用启动配置或任务,这个解释较长,请在需要时询问
- 或者如果您已全局安装了 tsx,运行
- 查看您的代码无限记录
undefined
- 修复
i < arr[i].length
为j < arr[i].length
英文:
- run
npm install tsx
- install typescript runner- or install it globally with
npm install --global tsx
- or install it globally with
- run
npx tsx watch main.ts
to run your file, in watch mode (restarting on file save)- or
tsx watch main.ts
if you've installed tsx globally - or with https://marketplace.visualstudio.com/items?itemName=rxliuli.tsx
- or with a launch configuration or tasks, that's long to explain ask if needed
- or
- see that your code infinitly logs
undefined
- fix
i < arr[i].length
toj < arr[i].length
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论