MST

星途 面试题库

面试题:TypeScript 元组类型的基础使用

请创建一个 TypeScript 元组,包含一个字符串和一个数字,并且编写一个函数,该函数接受此元组作为参数,将字符串和数字拼接成一个新的字符串并返回。例如传入 ['abc', 123] ,返回 'abc123'。
35.3万 热度难度
前端开发TypeScript

知识考点

AI 面试

面试题答案

一键面试
// 创建包含一个字符串和一个数字的元组类型
type StringNumberTuple = [string, number];

// 定义函数,接受元组作为参数并拼接成新字符串返回
function concatenateTuple(tuple: StringNumberTuple): string {
    return tuple[0] + tuple[1].toString();
}

// 测试函数
let myTuple: StringNumberTuple = ['abc', 123];
let result = concatenateTuple(myTuple);
console.log(result);