什么';这两者之间的区别是什么;建造商&引用;“静态”;TypeScript中的常规接口是什么?

什么';这两者之间的区别是什么;建造商&引用;“静态”;TypeScript中的常规接口是什么?,typescript,constructor,interface,static,Typescript,Constructor,Interface,Static,TypeScript有两个字符串接口,分别称为string和StringConstructor 此外,TypeScript在第1章第1.3节中提供了以下代码示例: interface JQuery { text(content: string); } interface JQueryStatic { get(url: string, callback: (data: string) => any); (query: string): JQuery; } JQuery/JQu

TypeScript有两个字符串接口,分别称为
string
StringConstructor

此外,TypeScript在第1章第1.3节中提供了以下代码示例:

interface JQuery {
  text(content: string);
}

interface JQueryStatic {
  get(url: string, callback: (data: string) => any);
  (query: string): JQuery;
}
JQuery
/
JQueryStatic
String
/
StringConstructor
接口和do
Static
Constructor
接口之间有什么区别


编辑:是的,我知道链接的书已经过时3年了。

我将从
String
切换到
String,因为
String
有褶皱,我稍后会提到*


实例接口(如
RegExp
JQuery
)通常表示可以存在多个不同实例的对象类型。关联的静态接口(如
RegExpConstructor
JQueryStatic
)通常表示创建或返回这些实例的对象类型;通常只有一个静态对象存在。因此,只有一个
RegExpConstructor
对象可以生成许多
RegExp
对象,只有一个
JQueryStatic
对象可以生成许多
JQuery
对象

在实践中,一个常见的困惑来源是。单个静态对象的名称(例如,
RegExp
jQuery
)往往与实例接口的名称相同。但是静态对象的类型不是实例接口的类型。因此,在运行时名为
RegExp
的值的类型为
RegExpConstructor
,而不是
RegExp
。运行时名为
jQuery
的值的类型为
JQueryStatic
,而不是
jQuery
。这是令人困惑的,但可能是最好的,因为它允许您在运行时说出类似于
x instanceof Y
的内容,并且在编译时
x
的类型是
Y

无论如何,如果有一个属性或方法的行为取决于一个特定的实例,它通常位于实例接口上。如果有某个属性或方法的行为不依赖于特定实例,则该属性或方法通常位于静态接口上


构造函数接口是一个静态接口,它专门允许您在其上使用来创建新实例。在TypeScript中,这类似于函数调用签名,但名称为
new
,如下所示:

type F = (x: string) => number[];
type C = new(x: string) => number[]; 
类型
F
表示一个函数,该函数采用
string
参数并生成一个
number
s数组,而类型
C
表示一个构造函数,该构造函数采用
string
参数并生成
number
s数组:

declare const f: F;
declare const c: C;
const arr1 = f("hey"); // number[]
const oops1 = new f("hey"); // error, f is not newable
const arr2 = new c("hey"); // number[]
const oops2 = c("hey"); // error, c is not callable
有一个静态接口也是一个构造函数接口是很常见的;所有
class
静态接口都是构造函数接口。但并不是每个静态接口都是构造函数接口。
JQueryStatic
接口就是一个不可用的例子。要从
JQueryStatic
对象获取
JQuery
实例,可以像调用函数一样调用它(这就是
(query:string):JQuery;
签名的意思)

这就是
JQueryStatic
/
JQuery
对和
RegExpConstructor
/
RegExp
对之间的主要区别,也是问题主要答案的结尾


*回到
字符串
褶皱。名为
String
的类型特别指的是通过调用
String
构造函数上的
new
操作符构造的对象。还有一个名为
string
(带小写字母“s”)的类型,它引用数据类型。实际上,您处理的所有字符串都是
string
类型的原语,而
string
是一种比较少见的类型,它包含
string
值。
字符串
字符串
通常是可互换的:

const stringPrimitive = "hello"; // type is string
const stringObject = new String("hello"); // type is String
console.log(stringPrimitive+"!"); // "hello!"
console.log(stringObject+"!"); // "hello!"
console.log(stringPrimitive.charAt(4)); // "o"
console.log(stringObject.charAt(4)); // "o"
console.log(typeof stringPrimitive); // "string"
console.log(typeof stringObject); // "object"
console.log(stringPrimitive instanceof String); // false
console.log(stringObject instanceof String); // true
除非它们不能互换:

const stringPrimitive = "hello"; // type is string
const stringObject = new String("hello"); // type is String
console.log(stringPrimitive+"!"); // "hello!"
console.log(stringObject+"!"); // "hello!"
console.log(stringPrimitive.charAt(4)); // "o"
console.log(stringObject.charAt(4)); // "o"
console.log(typeof stringPrimitive); // "string"
console.log(typeof stringObject); // "object"
console.log(stringPrimitive instanceof String); // false
console.log(stringObject instanceof String); // true
当您意识到
StringConstructor
也可以像函数一样调用,并生成一个原语
string
时,情况变得更加混乱:

console.log(typeof "hey"); // "string"
console.log(typeof new String("hey")); // "object"
console.log(typeof String("hey")); // "string"
所以这真是一团糟。这里的规则很简单。这就是为什么我将代码示例从
String
更改为始终是一个对象
RegExp
,对于这个对象,没有原始数据类型(
typeof/foo/==“object”
)来阻挡它



好的,希望能有帮助。祝你好运

谢谢你花时间和精力回答这个问题,我很感激。