为内部 class 创建类型?
Creating typings for inner class?
我正尝试在 Salesforce 项目中使用 typescript RemoteObject
但是,我不确定如何为外部语法创建类型 javascript object。
javascript中的代码使用示例:
var ct = new RemoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
让我感到困惑的部分是 new RemoteObjectModel.Contact()
。如何为此创建接口?
这是我要编译的代码示例
import React from 'react'
import ReactDOM from 'react-dom'
declare module RemoteObjectModel {
export class Account implements IRemoteObject {
constructor();
constructor();
get;
set;
}
}
interface IRemoteObject{
get(field: string): any;
set(field: string, value: string): any;
}
interface IAppProps{
remoteObjectModel: RemoteObjectModel
}
export class App extends React.Component<IAppProps, {}>{
public state : IAppState;
constructor(props : IAppProps) {
super(props);
this.state = {
accounts: 0
};
}
public render(){
let account: IRemoteObject = new this.props.remoteObjectModel.Account({
Name : 'hello!',
Active : true
});
console.log(account);
return (
<div>
New Accounts: {this.state.accounts}
Account: {account.get('Name')}
</div>
)
}
}
//set on the global state
declare var remoteObjectModel : IRemoteObjectModel;
ReactDOM.render(<App remoteObjectModel={remoteObjectModel} />, document.getElementById('app'));
这行不通。 IAppProps
有错误:
message: 'Cannot find name 'RemoteObjectModel'.'
此外,我不确定我的问题标题是否正确反映了问题(如果没有正确反映,请更新!)
如果 RemoteObjectModel
定义为 class,您确实不能在其中定义嵌套 class Contact
。
但是,您可以使用打字稿的 declaration merging 功能 - 您可以声明命名空间 RemoteObjectModel
以及同名的 class(或接口),并声明 class Contact
在该命名空间内(不要忘记 export
否则它将在命名空间之外不可见)。
然后,粗略地说,每当 typescript 单独看到 RemoteObjectModel
时,它会引用 class,而当它看到 RemoteObjectModel.something
时,它会引用命名空间。
像这样:
class RemoteObjectModel {
someProperty: string;
constructor() {
this.someProperty = 'someValue';
}
}
namespace RemoteObjectModel {
export class Contact {
FirstName: string;
LastName: string;
Phone: string;
constructor(values: ContactProperties) {
Object.assign(this, values);
}
}
}
interface ContactProperties {
FirstName: string;
LastName: string;
Phone: string;
}
var ct = new RemoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
此外,您可以在 RemoveObjectModel
class 中定义 constructor signature,这样您就可以调用 new remoteObjectModel.Contact()
,就好像 Contact
也在实例上定义了一样:
class RemoteObjectModel {
someProperty: string;
Contact: {new(p: ContactProperties): RemoteObjectModel.Contact}
}
namespace RemoteObjectModel {
export class Contact {
FirstName: string;
LastName: string;
Phone: string;
constructor(values: ContactProperties) {
Object.assign(this, values);
}
}
}
interface ContactProperties {
FirstName: string;
LastName: string;
Phone: string;
}
const remoteObjectModel = new RemoteObjectModel();
var c2 = new remoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
或者,您可以使用 typeof Contact
:
class RemoteObjectModel {
someProperty: string;
Contact: typeof RemoteObjectModel.Contact
constructor() {
this.Contact = RemoteObjectModel.Contact;
}
}
我正尝试在 Salesforce 项目中使用 typescript RemoteObject
但是,我不确定如何为外部语法创建类型 javascript object。
javascript中的代码使用示例:
var ct = new RemoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
让我感到困惑的部分是 new RemoteObjectModel.Contact()
。如何为此创建接口?
这是我要编译的代码示例
import React from 'react'
import ReactDOM from 'react-dom'
declare module RemoteObjectModel {
export class Account implements IRemoteObject {
constructor();
constructor();
get;
set;
}
}
interface IRemoteObject{
get(field: string): any;
set(field: string, value: string): any;
}
interface IAppProps{
remoteObjectModel: RemoteObjectModel
}
export class App extends React.Component<IAppProps, {}>{
public state : IAppState;
constructor(props : IAppProps) {
super(props);
this.state = {
accounts: 0
};
}
public render(){
let account: IRemoteObject = new this.props.remoteObjectModel.Account({
Name : 'hello!',
Active : true
});
console.log(account);
return (
<div>
New Accounts: {this.state.accounts}
Account: {account.get('Name')}
</div>
)
}
}
//set on the global state
declare var remoteObjectModel : IRemoteObjectModel;
ReactDOM.render(<App remoteObjectModel={remoteObjectModel} />, document.getElementById('app'));
这行不通。 IAppProps
有错误:
message: 'Cannot find name 'RemoteObjectModel'.'
此外,我不确定我的问题标题是否正确反映了问题(如果没有正确反映,请更新!)
如果 RemoteObjectModel
定义为 class,您确实不能在其中定义嵌套 class Contact
。
但是,您可以使用打字稿的 declaration merging 功能 - 您可以声明命名空间 RemoteObjectModel
以及同名的 class(或接口),并声明 class Contact
在该命名空间内(不要忘记 export
否则它将在命名空间之外不可见)。
然后,粗略地说,每当 typescript 单独看到 RemoteObjectModel
时,它会引用 class,而当它看到 RemoteObjectModel.something
时,它会引用命名空间。
像这样:
class RemoteObjectModel {
someProperty: string;
constructor() {
this.someProperty = 'someValue';
}
}
namespace RemoteObjectModel {
export class Contact {
FirstName: string;
LastName: string;
Phone: string;
constructor(values: ContactProperties) {
Object.assign(this, values);
}
}
}
interface ContactProperties {
FirstName: string;
LastName: string;
Phone: string;
}
var ct = new RemoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
此外,您可以在 RemoveObjectModel
class 中定义 constructor signature,这样您就可以调用 new remoteObjectModel.Contact()
,就好像 Contact
也在实例上定义了一样:
class RemoteObjectModel {
someProperty: string;
Contact: {new(p: ContactProperties): RemoteObjectModel.Contact}
}
namespace RemoteObjectModel {
export class Contact {
FirstName: string;
LastName: string;
Phone: string;
constructor(values: ContactProperties) {
Object.assign(this, values);
}
}
}
interface ContactProperties {
FirstName: string;
LastName: string;
Phone: string;
}
const remoteObjectModel = new RemoteObjectModel();
var c2 = new remoteObjectModel.Contact({
FirstName: "Aldo",
LastName: "Michaels",
Phone: "(415) 555-1212"
});
或者,您可以使用 typeof Contact
:
class RemoteObjectModel {
someProperty: string;
Contact: typeof RemoteObjectModel.Contact
constructor() {
this.Contact = RemoteObjectModel.Contact;
}
}