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'.
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;
};
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;
};
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
- 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
- TypeScript
非空断言
运算符 (Postfix !)!
Non-null Assertion Operator
Postfix !
function liveDangerously(x?: number | null) {
// No error
console.log(x!.toFixed());
}
https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions
- 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]
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, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18222160
未经授权禁止转载,违者必究!