有人知道TypeScript中String和String的区别吗?我假设它们应该是一样的,对吗?

var a: String = "test";
var b: string = "another test";
a = b;
b = a; // this gives a compiler error!

当前版本的编译器说:

Type 'String' is not assignable to type 'string'.
  'string' is a primitive, but 'String' is a wrapper object.
     Prefer using 'string' when possible.

那是虫子吗?


当前回答

这两种类型在JavaScript和TypeScript中是截然不同的——TypeScript只是提供了注释和检查类型的语法。

String引用具有String的对象实例。原型链中的原型。你可以通过多种方式获得这样的实例,例如new String('foo')和Object('foo')。你可以用instanceof操作符来测试String类型的实例,例如myString instanceof String。

字符串是JavaScript的基本类型之一,字符串值主要是用文字创建的。'foo'和"bar",以及作为各种函数和操作符的结果类型。你可以使用typeof myString === 'string'来测试字符串类型。

The vast majority of the time, string is the type you should be using - almost all API interfaces that take or return strings will use it. All JS primitive types will be wrapped (boxed) with their corresponding object types when using them as objects, e.g. accessing properties or calling methods. Since String is currently declared as an interface rather than a class in TypeScript's core library, structural typing means that string is considered a subtype of String which is why your first line passes compilation type checks.

其他回答

TypeScript:字符串vs字符串

String类型的实参不能赋值给String类型的形参。 'string'是一个原语,但'string'是一个包装器对象。 尽可能使用'string'。

demo

字符串对象

// error
class SVGStorageUtils {
  store: object;
  constructor(store: object) {
    this.store = store;
  }
  setData(key: String = ``, data: object) {
    sessionStorage.setItem(key, JSON.stringify(data));
  }
  getData(key: String = ``) {
    const obj = JSON.parse(sessionStorage.getItem(key));
  }
}

字符串的原始

// ok
class SVGStorageUtils {
  store: object;
  constructor(store: object) {
    this.store = store;
  }
  setData(key: string = ``, data: object) {
    sessionStorage.setItem(key, JSON.stringify(data));
  }
  getData(key: string = ``) {
    const obj = JSON.parse(sessionStorage.getItem(key));
  }
}

JavaScript有7种基本类型字符串,数字,布尔值,null, undefined, symbol和bigint,前五种从一开始就存在,符号原语是在ES2015中添加的,bigint正在最终确定的过程中。

原语与对象的区别在于它是不可变的并且没有方法。你可能会反对字符串有方法

 console.log('primitive'.charAt(3)) // output is "m"

虽然字符串原语没有方法,但JavaScript也定义了string对象类型。JavaScript可以在这些类型之间自由转换。当您在字符串原语上访问charAt等方法时,JavaScript将其包装在string对象中,调用该方法,然后丢弃该对象。

TypeScript通过为原语及其对象包装器提供不同的类型来模拟这种区别:

字符串和字符串 数目和数目 布尔和布尔 符号与符号 bigint和bigint

通常情况下,如果你使用String而不是String,事情会正常工作,除了在某些情况下,编译器会引发一个错误

如您所见,string可以赋值给string,但string不能赋值给string。

按照错误消息中的建议使用字符串。TypeScript附带的所有类型声明都使用它,几乎所有其他库的类型也是如此。

在JavaScript中,字符串既可以是字符串基本类型,也可以是字符串对象。下面的代码显示了两者的区别:

var a: string = 'test'; // string literal
var b: String = new String('another test'); // string wrapper object

console.log(typeof a); // string
console.log(typeof b); // object

你的错误:

类型'String'不能赋值给类型'String'。'string'是a 但'String'是一个包装器对象。 尽可能使用'string'。

由TS编译器抛出,因为您试图将类型字符串分配给字符串对象类型(通过new关键字创建)。编译器告诉你,你应该只对字符串基本类型使用类型string,你不能用这个类型来描述字符串对象类型。

此错误消息表明您正在尝试将类型为String的值分配给类型为String的变量或参数。在TypeScript中,string是一个表示字符序列的基本类型,而string是一个包装器对象,它提供了处理字符串的额外方法。

错误消息建议尽可能使用字符串类型而不是string,因为字符串类型效率更高,相关开销更少。这是因为String是一个对象,而JavaScript(因此TypeScript)中的对象存储在堆上,创建时需要额外的内存分配。相比之下,字符串值直接存储在堆栈上,这使得它们更有效地使用。

要修复此错误,您需要确保使用的是string类型,而不是期望使用的string类型。只需在代码中用String替换String就可以做到这一点。例如,如果你有一个String类型的变量,你可以把它改成这样的String类型:

let myString: String = "hello, world!"; // Incorrect
let myString: string = "hello, world!"; // Correct

还值得注意的是,通常情况下,您应该尽可能使用string而不是string,因为它更有效,更容易使用。

简单的回答是:

String =>是一个类型。例如console.log(typeof 'foo') //字符串 String =>是一个具有一些创建和操作字符串的方法的对象。