type 'regexp' is not assignable to type 'number' 这个错误信息意味着你试图将一个正则表达式的实例(regexp 类型)赋值给一个期望为数字(number 类型)的变量或属性。在类型安全的编程语言中,如 TypeScript,这种类型不匹配的操作是不被允许的,因为正则表达式和数字是两种完全不同的数据类型。 2. 分析为何不能赋...
} public getUrl(url:string):string{ return this.TargetUrl+url; } There is an error messageArgument of type 'string is not assignable to parameter of type RegExp. However, no matter what I put at<--errorspot, it will show the same error. If I replace callingthis.getUrl(url)to a pu...
functionnumberToString<T>(value:T):Exclude<T,number>{returntypeofvalue==='number'?`${value}`:value;// ^^^ Error}// usagefunctionselectText(text:string|RegExp){// todo}functionanotherFn(arg:number|string|RegExp){// this is actually working, but somehow Line 2 is still raising the erro...
functionfn(x:string) {console.log("Hello, "+ x.toLowerCase()); }typeStringOrNumberFunc=(ns:string|number) =>void;letfunc:StringOrNumberFunc= fn;// 不能将类型“(x: string) => void”分配给类型“StringOrNumberFunc”。// 参数“x”和“ns” 的类型不兼容。// 不能将类型“string | num...
就是很简单的从一个数组里拿参数塞给 replace。 问题在于我 syntax 要么是 [ RegExp, string ][],要么是 [ RegExp, (...arg: string[]) => string ],只要我写成联合类型他就报错
let num1:number[] = [1,2,3,'4'] // TS2322: Type 'string' is not assignable to type 'number'. 数组的一些方法也会对类型有限制。 let num: number[] = [1,2,3,4,5,6] num.push('1') // TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. ...
error TS2411: Property 'id' of type 'number | undefined' is not assignable to string index type 'string'. 这是因为接口上的一些属性是可选的,可能是未定义的,并且类型并不总是字符串(例如id是一个数字)。 我们可以尝试用联合类型来解决这个问题,这是一种TypeScript语法,用来定义两个或更多其他类型之间...
let isHandsome: boolean = true 1.赋值与定义的不一致,会报错,静态类型语言的优势就体现出来了,可以帮助我们提前发现代码中的错误。 let isHandsome: boolean = 'true' // Type 'string' is not assignable to type 'boolean'. 1. 2.number let age: number = 18 1.string...
Math.pow(10, '2'); // index.ts(1,14): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.上面的例子中,Math.pow 必须接受两个 number 类型的参数。事实上 Math.pow 的类型定义如下:interface Math { /** * Returns the value of a base expression ...
boolean、number 和 string 类型boolean let isDone: boolean = false; // let isDone: boolean = "123"; //Type 'string' is not assignable to type 'boolean' 赋值与定义的不一致,会报错,静态类型语言的优势就体现出来了,可以帮助我们提前发现代码中的错误。