Generics 在打字稿中,“新”是什么意思?
我在typescript中有以下代码片段:Generics 在打字稿中,“新”是什么意思?,generics,typescript,Generics,Typescript,我在typescript中有以下代码片段: interface IFoo { new?():string; } class Foo implements IFoo { new() { return 'sss'; } } 我必须把它放进去吗?在接口方法new中,否则将显示编译错误。我不明白为什么 更新: 原来的问题并不是一种使用新方法的好方法,因此我发布了一个更现实、更实用的方法来使用上述新方法: interface IUser { id: numb
interface IFoo {
new?():string;
}
class Foo implements IFoo {
new() {
return 'sss';
}
}
我必须把它放进去吗?在接口方法new中,否则将显示编译错误。我不明白为什么
更新:
原来的问题并不是一种使用新方法的好方法,因此我发布了一个更现实、更实用的方法来使用上述新方法:
interface IUser {
id: number;
name: string;
}
interface UserConstructable {
new (id: number, name: string, ...keys: any[]): IUser;
}
class User implements IUser {
constructor(public id: number, public name: string) {
}
}
class Employee implements IUser {
constructor(public id: number, public name: string,
private manager: string) {
}
}
class UserFactory {
private static _idx: number = 0;
static makeUser(n: UserConstructable): IUser {
let newId = UserFactory._idx++;
return new n(newId, 'user-' + newId, 'manager-' + newId);
}
}
console.log(UserFactory.makeUser(User));
console.log(UserFactory.makeUser(User));
console.log(UserFactory.makeUser(Employee));
让我们把它分成几个部分。。。我知道你已经知道其中一些,但我正在努力做到完整
1..2.3..4
new?():string;
在这个特定的上下文中,new不是一个关键字,它只是一个方法名。。。由于此列表中的下一项
那个?使之成为可选的,即您可以实现接口而不实现此方法
新的手段是一种方法
返回类型是字符串
范例
class Foo implements IFoo {
new() {
return 'sss';
}
}
class Bar implements IFoo {
// Because new is optional, you don't have to implement it
}
如果省略?,则使用new作为关键字,但TypeScript将示例中的代码解释为一个简单的方法
不过,总的来说,我不会用保留字来命名成员。让我们把它分成几个部分。。。我知道你已经知道其中一些,但我正在努力做到完整
1..2.3..4
new?():string;
在这个特定的上下文中,new不是一个关键字,它只是一个方法名。。。由于此列表中的下一项
那个?使之成为可选的,即您可以实现接口而不实现此方法
新的手段是一种方法
返回类型是字符串
范例
class Foo implements IFoo {
new() {
return 'sss';
}
}
class Bar implements IFoo {
// Because new is optional, you don't have to implement it
}
如果省略?,则使用new作为关键字,但TypeScript将示例中的代码解释为一个简单的方法
但总的来说,我会避免用保留字命名成员。在javascript中,函数是对象,因此typescript接口也应该能够匹配函数。这就是为什么我们会有那些奇怪的界面
interface SomeFunc {
(key: string): boolean;
(key: number): boolean;
}
interface IFoo {
new (key: string): {key:string};
}
function someFunction(key: string) { // doesn't match IFoo
return {key};
}
var someObject = { // still doesn't match IFoo
new(key: string) {
return {key};
},
};
function MyClass(key: string) { // matches IFoo! must be called like `new MyClass()`
this.key = key;
}
该接口将只匹配接受字符串或数字并返回布尔值的函数
现在考虑构造函数,即ES5~./P>中的类的情况。
function MyClass() {
this.prop = value;
}
let myClass = new MyClass;
我们需要一种方法来创建一个匹配MyClass函数签名的接口。这就是我们在接口中获得新函数的地方
interface SomeFunc {
(key: string): boolean;
(key: number): boolean;
}
interface IFoo {
new (key: string): {key:string};
}
function someFunction(key: string) { // doesn't match IFoo
return {key};
}
var someObject = { // still doesn't match IFoo
new(key: string) {
return {key};
},
};
function MyClass(key: string) { // matches IFoo! must be called like `new MyClass()`
this.key = key;
}
因此,可以说new函数的签名必须与类构造函数的签名相匹配
interface IFoo {
new(key: string): {};
}
class Foo implements IFoo { // works ok
constructor(key: string) { // you can't return anything
}
}
我想你想做的是:
interface IFoo {
new: () => string;
}
i、 e.IFoo应该有一个名为new的属性,这是一个返回字符串的函数。在javascript中,函数是对象,因此typescript接口也应该能够匹配函数。这就是为什么我们会有那些奇怪的界面
interface SomeFunc {
(key: string): boolean;
(key: number): boolean;
}
interface IFoo {
new (key: string): {key:string};
}
function someFunction(key: string) { // doesn't match IFoo
return {key};
}
var someObject = { // still doesn't match IFoo
new(key: string) {
return {key};
},
};
function MyClass(key: string) { // matches IFoo! must be called like `new MyClass()`
this.key = key;
}
该接口将只匹配接受字符串或数字并返回布尔值的函数
现在考虑构造函数,即ES5~./P>中的类的情况。
function MyClass() {
this.prop = value;
}
let myClass = new MyClass;
我们需要一种方法来创建一个匹配MyClass函数签名的接口。这就是我们在接口中获得新函数的地方
interface SomeFunc {
(key: string): boolean;
(key: number): boolean;
}
interface IFoo {
new (key: string): {key:string};
}
function someFunction(key: string) { // doesn't match IFoo
return {key};
}
var someObject = { // still doesn't match IFoo
new(key: string) {
return {key};
},
};
function MyClass(key: string) { // matches IFoo! must be called like `new MyClass()`
this.key = key;
}
因此,可以说new函数的签名必须与类构造函数的签名相匹配
interface IFoo {
new(key: string): {};
}
class Foo implements IFoo { // works ok
constructor(key: string) { // you can't return anything
}
}
我想你想做的是:
interface IFoo {
new: () => string;
}
i、 e.IFoo应该有一个名为new的属性,这是一个返回字符串的函数。它不只是函数的名称吗?您是否尝试给实现一个显式的返回类型?它可能默认为any。@Trygve Skogsholm,我不确定您的评论。如果我将方法名从new更改为其他名称,比如重新,删除?from接口不会导致编译错误。嗯,也许new只能返回Foo?@Trygve Skogsholm,我想你是对的。我稍微挖掘了一下,发现这可能是相关的:它不只是函数的一个名称吗?您是否尝试给实现一个显式的返回类型?它可能默认为any。@Trygve Skogsholm,我不确定您的评论。如果我将方法名从new更改为其他名称,比如重新,删除?from接口不会导致编译错误。嗯,也许new只能返回Foo?@Trygve Skogsholm,我想你是对的。我挖了一点,发现这可能是相关的:谢谢你的详细答复。我有一点要指出的是,与你的第一条评论相反,new在typescript中有一些特殊的含义,尽管它可能不是一个关键字。否则,如果将其替换为新的方法名,并且是否存在?不要紧,从编译的角度来看。是什么?是你问题中最重要的部分。用这个?在这个界面中,new确实是一个关键字,但是当你附加?它改变了编译器解释声明的方式,new变成了一个简单的方法,而不是关键字。感谢您的详细回复。我有一点要指出的是,与你的第一条评论相反,new在typescript中有一些特殊的含义,尽管它可能不是一个关键字。否则,如果将其替换为新的方法名,并且是否存在?不要紧,从编译的角度来看。是什么?是你问题中最重要的部分。用这个?在这个界面中,new确实是一个关键字,但是当你附加?我 t改变了编译器解释声明的方式,new变成了一个简单的方法而不是关键字。