xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript error TS2345 All In One

TypeScript error TS2345 All In One

error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

image

errors

function dailyTemperatures(temperatures: number[]): number[] {
  // ❌ error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  let answer = [...``.padEnd(temperatures.length, 0)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(i => parseInt(i))!;
  //  ✅
  // let answer = new Array(temperatures.length).fill(0);
  let stack: number[] = [];
  for(let i = 0; i < temperatures.length; i++) {
    while(stack.length && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      let index = stack.pop();
      answer[index] = i - index;
    }
    stack.push(i);
  }
  return answer;
};

https://leetcode.com/problems/daily-temperatures/

https://github.com/SharePoint/sp-dev-training-spfx-webpart-proppane/issues/7#issuecomment-2138695778

solutions

0 => '0', number 替换成 string ✅

function dailyTemperatures(temperatures: number[]): number[] {
  // ❌ error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  let answer = [...``.padEnd(temperatures.length, `0`)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(i => parseInt(i))!;
  //  ✅
  // let answer = new Array(temperatures.length).fill(0);
  let stack: number[] = [];
  for(let i = 0; i < temperatures.length; i++) {
    while(stack.length && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      let index = stack.pop();
      answer[index] = i - index;
    }
    stack.push(i);
  }
  return answer;
};

https://www.typescriptlang.org/play/?ssl=17&ssc=3&pln=1&pc=1#code/GYVwdgxgLglg9mABAEwIYwDYE8AqBTAWwAc8AnVKEUvAZwAopCTzLqaAuRMEAgIzIDaAXQCUnbn0FDEAbwBQiRAHoliQDLkiMqTilEOAMoAmAMwAWAKycAgqQDmPPGCiI4wRFCwlEAcgn9S3ogwNFxwzqg0NDC2YKi8GHjucIhEqOQEeIy6ru6eid40UKQwYLbeAHQKiAnhYDQA7mSIALyIAuUdAAad5anIAKJgyAxMZBRUtOUJpVAAFgA0iJ0ADJ0iQuUEqER0AHI8-iIA3FUq1ZmIqHWNuq3tXT19g8OMxGOsk9O2c4vL65vbPYHMjHU6qGqXa5NO4dcrdXqoAZDEZvFgTGhTRzfBaIP4bLY7GAtAB8KTSNDwAEknHQYCIRABCE6KM6IQCg5GDzrUGtCuHh6ogbOQsCjmOM2JiZrMROVgJgMHQ-syuYhCqgIABrcTA0jCFptITK4A6OgQomtZZHIKIAA87lGaIlXzmVpgAGo3SJZFVFPVZpg8HQ1ZrJdjEAAycP21Hi2gCYMa+NQdUa0NzRAAWkQAEYhNI7a8xR8aAIYKJvYpKyqSsg8AAPfUJ3pwHagquKK483U1+vSVpErM9uvKxQAXx9quTIaIIBos1pbcQ48U1FYSE7NxOo6OQA

function dailyTemperatures(temperatures: number[]): number[] {
  // ❌ error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  let answer = [...``.padEnd(temperatures.length, `0`)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(i => parseInt(i))!;
  //  ✅
  // let answer = new Array(temperatures.length).fill(0);
  let stack: number[] = [];
  for(let i = 0; i < temperatures.length; i++) {
    while(stack.length && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      //  ✅
      let index = stack.pop()!;
      // ❌ Type 'undefined' cannot be used as an index type.
      // ❌ 'index' is possibly 'undefined'.
      // let index = stack.pop();
      answer[index] = i - index;
    }
    stack.push(i);
  }
  return answer;
};

https://www.typescriptlang.org/play/?#code/GYVwdgxgLglg9mABAEwIYwDYE8AqBTAWwAc8AnVKEUvAZwAopCTzLqaAuRMEAgIzIDaAXQCUnbn0FDEAbwBQiRAHoliQDLkiMqTilEOAMoAmAMwAWAKycAgqQDmPPGCiI4wRFCwlEAcgn9S3ogwNFxwzqg0NDC2YKi8GHjucIhEqOQEeIy6ru6eid40UKQwYLbeAHQKiAnhYDQA7mSIALyIAuUdAAad5anIAKJgyAxMZBRUtOUJpVAAFgA0iJ0ADJ0iQuUEqER0AHI8-iIA3FUq1ZmIqHWNuq3tXT19g8OMxGOsk9O2c4vL65vbPYHMjHU6qGqXa5NO4dcrdXqoAZDEZvFgTGhTRzfBaIP4bLY7GAtAB8KTSNDwAEknHQYCIRABCE6KM6IQCg5GDzrUGtCuHh6ogbOQsCjmOM2JiZrMROVgJgMHQ-syuYhCqgIABrcTA0jCFptITK4A6OgQomtZZHIKIAA87lGaIlXzmVpgAGo3SJZFVFPVZpg8HQ1ZrJdjEAAycP21Hi2gCYMa+NQdUa0NzRAAWkQAEYhNI7a8xR8aAIYKJvYpKyqSsg8AAPfUJ3pwHaM5VVs4aHB5HzgWtysB4ZCBCBXMBhRD8RAgCnIS4hK5BIb13IkSpVlmqDTeGv1wLBFJwSIweJYXvLgdDio+ytnM3LhutJtEFt0UEbyE83W7uvSVpErMf3bRAAF8b2fGdZlpd9QKqahWCQK4vxOECjiAA

demos

function dailyTemperatures(temperatures: number[]): number[] {
  // ❌ error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
  let answer = [...``.padEnd(temperatures.length, `0`)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(Number);
  // let answer = [...``.padEnd(temperatures.length, 0)].map(i => parseInt(i))!;
  //  ✅
  // let answer = new Array(temperatures.length).fill(0);
  let stack: number[] = [];
  for(let i = 0; i < temperatures.length; i++) {
    while(stack.length && temperatures[stack[stack.length - 1]] < temperatures[i]) {
      //  ✅
      let index = stack.pop()!;
      // ❌ Type 'undefined' cannot be used as an index type.
      // ❌ 'index' is possibly 'undefined'.
      // let index = stack.pop();
      answer[index] = i - index;
    }
    stack.push(i);
  }
  return answer;
};

https://www.typescriptlang.org/play/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

TypeScript

  1. TypeScript 类型断言 as

Type Assertions

const myCanvas = document.getElementById("main_canvas") as HTMLCanvasElement;

// @errors: 2352
const x = "hello" as number;
// ❌ Conversion of type 'string' to type 'number' may be a mistake because neither type sufficiently overlaps with the other. 
// ❌ If this was intentional, convert the expression to 'unknown' first.
// ✅
const x = "hello" as unknown;

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#type-assertions

https://www.typescriptlang.org/docs/handbook/2/objects.html#excess-property-checks

https://www.typescriptlang.org/docs/handbook/2/objects.html#generic-object-types

https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions

  1. TypeScript 非空断言运算符 (Postfix !) !

Non-null Assertion Operator Postfix !

function liveDangerously(x?: number | null) {
  // No error
  console.log(x!.toFixed());
}

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-

https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions

  1. TypeScript 强制类型转换

js string padEnd

// ✅
[...``.padEnd(7, `0`)].map(Number);
// (7) [0, 0, 0, 0, 0, 0, 0]
[...``.padEnd(7, ` `)].map(Number);
// (7) [0, 0, 0, 0, 0, 0, 0]

// ⚠️
[...``.padEnd(7, 0)].map(Number);
// (7) [0, 0, 0, 0, 0, 0, 0]

// ❌
[...``.padEnd(7, ``)].map(Number);
// []
[...``.padEnd(7, `1`)].map(Number);
// (7) [1, 1, 1, 1, 1, 1, 1]
[...``.padEnd(7, `x`)].map(Number);
// (7) [NaN, NaN, NaN, NaN, NaN, NaN, NaN]

image

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd

refs

https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1568

https://www.cnblogs.com/xgqfrms/tag/TypeScript Bang!/



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-05-30 13:24  xgqfrms  阅读(68)  评论(1编辑  收藏  举报