为什么TypeScript让我将字符串原语赋值给类型为String (对象类型)的变量?例如,为什么这不会导致错误:
var str : String = '2333';
请注意,另一种方法不起作用:
var str : string = new String('2333'); // Error: Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper o
我正在尝试重载/重写TypeScript中的类方法,其中上级方法使用零参数,而从属方法使用一个参数。
我的TypeScript代码类似于以下内容:
class Super {
create(): void {}
}
class Sub extends Super {
create(arg: object): void {}
}
const sub = new Sub();
sub.create({});
此代码产生以下错误:
Property 'create' in type 'Sub' is not assignable to the same p
我有一个代码,看起来如下(T是一个通用的):
if (!(item as Exclude<T, null | undefined>).hasOwnProperty(this.idKey)) {
throw new Error("...");
}
我得到一个TypeScript错误:
TS2339: Property 'hasOwnProperty' does not exist on type 'Exclude '.
如果hasOwnProperty是Object类的一个方法,那么这种类型怎么可能没有Object呢?在null
我在客户端使用TypeScript,当我运行应用程序时,有两个错误显示如下:
@typescript-eslint/no-unsafe-assignment: Unsafe assignment of an `any` value.
@typescript-eslint/no-unsafe-member-access: Unsafe member access .value on an `any` value.
这是我的代码:
const userInfo = ref({} as UserInfo) // first error
$f.axios
.get<UserInfo&
我正在处理angular2 typescript项目,我只想在调用angular2()函数后将对象的键重置为'‘,示例如下
export class Events{
event:any={ name : '', date : '',.........};
eventOriginal=this.event;
save(){
//save(this.event)
//after save, reset value to initial state
this.event=this.eventO
我尝试使用std::type_traits对类型进行静态分析(如TypeScript ),并首先创建了is_same<>。类和基元类型可以正常工作,但是字符串文字类型有奇怪的行为。
type is_same<A, B> = A extends B ? B extends A ? true : false : false;
type _1 = is_same<"a" | "b", "a" | "b">;
type _2 = "a" | "b" extends
我试图拥有一个Adapter类,它有一个函数指针(比如fnPtr)。并根据不同的适配类,给fnPtr分配相应的适配功能。下面是代码片段:
class AdapteeOne
{
public:
int Responce1()
{
cout<<"Respose from One."<<endl;
return 1;
}
};
class AdapteeTwo
{
public:
int Responce2()
{
cout<<"Respose from Two."&
我对TypeScript编译器有问题。为什么下面的代码片段没有显示任何错误?
class A {
}
class B {
myVar:string;
}
class Foo {
bar(a:A){}
doSomething(){
this.bar(new B());
}
}
既然B不是A,不应该禁止带B的酒吧吗?