402

有人知道TypeScriptStringstringTypeScript 之间的区别吗?我是否正确假设它们应该是相同的?

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.

那是一个错误吗?

4

7 回答 7

373

这是一个显示差异的示例,这将有助于解释。

var s1 = new String("Avoid newing things where possible");
var s2 = "A string, in TypeScript of type 'string'";
var s3: string;

String是 JavaScript 字符串类型,您可以使用它来创建新字符串。没有人这样做,因为在 JavaScript 中文字被认为更好,因此s2在上面的示例中创建了一个新字符串,而不使用new关键字,也没有显式使用String对象。

string是 TypeScript 字符串类型,可以用来输入变量、参数和返回值。

补充笔记...

目前(2013 年 2 月)两者s1s2都是有效的 JavaScript。s3是有效的 TypeScript。

的使用String。您可能永远不需要使用它,字符串文字被普遍认为是初始化字符串的正确方法。在 JavaScript 中,使用对象字面量和数组字面量也被认为更好:

var arr = []; // not var arr = new Array();
var obj = {}; // not var obj = new Object();

如果你真的喜欢字符串,你可以在 TypeScript 中以两种方式之一使用它......

var str: String = new String("Hello world"); // Uses the JavaScript String object
var str: string = String("Hello World"); // Uses the TypeScript string type
于 2013-02-06T11:04:06.760 回答
76

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

String refers to an object instance that has String.prototype in its prototype chain. You can get such an instance in various ways e.g. new String('foo') and Object('foo'). You can test for an instance of the String type with the instanceof operator, e.g. myString instanceof String.

string is one of JavaScript's primitive types, and string values are primarily created with literals e.g. 'foo' and "bar", and as the result type of various functions and operators. You can test for string type using 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.

于 2016-01-25T13:19:16.903 回答
13

For quick readers:

Don’t ever use the types Number, String, Boolean, Symbol, or Object These types refer to non-primitive boxed objects that are almost never used appropriately in JavaScript code.

source: https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html

于 2020-05-29T07:56:41.683 回答
11

In JavaScript strings can be either string primitive type or string objects. The following code shows the distinction:

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

Your error:

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

Is thrown by the TS compiler because you tried to assign the type string to a string object type (created via new keyword). The compiler is telling you that you should use the type string only for strings primitive types and you can't use this type to describe string object types.

于 2019-07-19T12:33:59.397 回答
6

TypeScript: String vs string

Argument of type 'String' is not assignable to parameter of type 'string'.

'string' is a primitive, but 'String' is a wrapper object.

Prefer using 'string' when possible.

demo

String Object

// 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));
  }
}

string primitive

// 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));
  }
}

enter image description here

于 2020-02-15T04:57:41.063 回答
5

Simple answer:

  • string => is a type. e.g. console.log(typeof 'foo') // string
  • String => is an object with some methods to create and manipulate strings.
于 2021-07-21T23:15:07.807 回答
0

Based on my personal reference

Prefer the string over the String

于 2022-02-07T12:39:11.520 回答