什么';这两者之间的区别是什么;建造商&引用;“静态”;TypeScript中的常规接口是什么?
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
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
接口和doStatic
与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”
)来阻挡它
好的,希望能有帮助。祝你好运 谢谢你花时间和精力回答这个问题,我很感激。