TypeScript declare Object Array Interface methods All In One
TypeScript declare Object Array Interface methods All In One
extends Array<ObjectType>
// type alias
type ObjectType = {
// input: [];
// input: any[];
input: [number[], number];
result: number[];
desc: string;
}
// 1. TypeScript & define Object Array Interface methods ✅ extends Array<ObjectType>
interface TestCaseInterface extends Array<ObjectType> {
//
}
// 测试用例 test cases
const testCases: TestCaseInterface = [
{
input: [
[1,2,3,4,5,6,7],
3,
],
result: [5,6,7,1,2,3,4],
desc: 'value equal to [5,6,7,1,2,3,4]',
},
{
input: [
[-1,-100,3,99],
2,
],
result: [3,99,-1,-100],
desc: 'value equal to [3,99,-1,-100]',
},
];
// Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
for (const [i, testCase] of testCases.entries()) {
// const [first, second] = testCase.input;
// Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
const [first, second] = testCase.input;
rotate(first, second);
// Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
}
extends Array<any>
& [index: number]: ObjectType;
// type alias
type ObjectType = {
// input: [];
// input: any[];
input: [number[], number];
result: number[];
desc: string;
}
// 2. TypeScript & define Object Array Interface methods ✅ [index: number]: ObjectType;
interface TestCaseInterface extends Array<any> {
[index: number]: ObjectType;
}
// 测试用例 test cases
const testCases: TestCaseInterface = [
{
input: [
[1,2,3,4,5,6,7],
3,
],
result: [5,6,7,1,2,3,4],
desc: 'value equal to [5,6,7,1,2,3,4]',
},
{
input: [
[-1,-100,3,99],
2,
],
result: [3,99,-1,-100],
desc: 'value equal to [3,99,-1,-100]',
},
];
// Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
for (const [i, testCase] of testCases.entries()) {
// const [first, second] = testCase.input;
// Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
const [first, second] = testCase.input;
rotate(first, second);
// Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
}
Property 'entries' does not exist on type ''.ts(2339)
declare global {
interface FormData {
entries(): Iterator<[USVString, USVString | Blob]>;
}
}
Type '' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
tsconfig.json
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"lib": [
"ES2015",
"ES2016",
"ES2017",
"ES2018",
"ES2019",
"ES2020",
"ES2021",
"ES2022",
"dom",
"dom.iterable"
],
}
}
leetcode demo
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2022-08-10
* @modified
*
* @description 89. Rotate Array
* @description 189. 旋转数组
* @difficulty Medium
* @ime_complexity O(n)
* @space_complexity O(n)
* @augments
* @example
* @link https://leetcode.com/problems/rotate-array/
* @link https://leetcode-cn.com/problems/rotate-array/
* @solutions
*
* @best_solutions
*
*/
export {};
const log = console.log;
/**
Do not return anything, modify nums in-place instead.
*/
// solution 2: in-place 就地交换算法 ✅
function reverse(i: number, j: number, nums: number[]): void {
while(i < j){
[
nums[i],
nums[j]
] = [
nums[j],
nums[i]
];
// 两两互换
i++;
j--;
}
};
function rotate(nums: number[], k: number): void {
k %= nums.length;
// 如果 k 大于 nums.length 则完成一个循环,这意味着它将保持不变,我们必须进行剩余移位
// reverse all
reverse(0, nums.length-1, nums);
// reverse first part
reverse(0, k - 1, nums)
// reverse second part
reverse(k, nums.length-1, nums);
};
/*
Line 10: Char 22: error TS2462: A rest element must be last in a destructuring pattern.
*/
// type alias
type ObjectType = {
// input: [];
// input: any[];
input: [number[], number];
result: number[];
desc: string;
}
// 1. TypeScript & define Object Array Interface methods ✅ extends Array<ObjectType>
// interface TestCaseInterface extends Array<ObjectType> {
// //
// }
// 2. TypeScript & define Object Array Interface methods ✅ [index: number]: ObjectType;
interface TestCaseInterface extends Array<any> {
[index: number]: ObjectType;
}
// 测试用例 test cases
const testCases: TestCaseInterface = [
{
input: [
[1,2,3,4,5,6,7],
3,
],
result: [5,6,7,1,2,3,4],
desc: 'value equal to [5,6,7,1,2,3,4]',
},
{
input: [
[-1,-100,3,99],
2,
],
result: [3,99,-1,-100],
desc: 'value equal to [3,99,-1,-100]',
},
];
// Property 'entries' does not exist on type 'TestCaseInterface'.ts(2339)
for (const [i, testCase] of testCases.entries()) {
// const [first, second] = testCase.input;
// Type '{ nums: number[]; k: number; }' must have a '[Symbol.iterator]()' method that returns an iterator.ts(2488)
const [first, second] = testCase.input;
rotate(first, second);
// Argument of type 'number[]' is not assignable to parameter of type 'number'.ts(2345)
log(`test case i result: \n`, first.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, testCase.result);
// const result = rotate(first, second);
// log(`test case i result: \n`, result.join('') === testCase.result.join('') ? `passed ✅` : `failed ❌`, result);
// log(`test case i =`, testCase);
}
// solution 1:暴力破解:❌ Time Limit Exceeded
// function rotate(nums: number[], k: number): void {
// if(k === 0) {
// // return;
// } else {
// const len = nums.length;
// while(k > 0) {
// // deep clone
// let temp = [];
// for(let i= 0; i< len; i++) {
// // 破解对象引用 bug, 值引用
// temp[i] = nums[i];
// }
// let end = temp[len - 1];
// // 循环队列
// for(let i = 1; i< len; i++) {
// // 交换swap
// nums[i] = temp[i - 1];
// }
// nums[0] = end;
// // console.log(`nums =`, nums);
// k -= 1;
// }
// }
// // no return, in-place 就地交换
// };
refs
https://github.com/xgqfrms/leetcode/blob/master/js-solutions/189 rotate-array.ts
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16576871.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
2021-08-11 vanilla js get string's bytes length All In One
2021-08-11 vue scss @import scoped bug All In One
2021-08-11 vue get slot props All In One
2020-08-11 如何重置电信悦 me 智能网关 All In One
2020-08-11 图解 Webpack 4.x 热更新原理 All In One
2020-08-11 Raspberry Pi 电路图模拟器 All In One
2019-08-11 IndexedDB & bug