定义闭合函数的返回类型的更好方法
我正在在Typescript定义文件.D.TS中声明该函数。它返回一个复杂的预定义对象。需要同样的帮助。
我使用了npx -p typeScript TSC SRC/Handler.js - declaration -declaration -allowjs -emitdeclarationly -outdir types
命令类型,它为我提供了一个.d.ts文件。 js是以下类似的内容:
handler.js
function handler() {
const myconst = 10;
function setupHandler (config) {
config.value = myconst;
return { ...config }
}
function setProcess(cnf) {
let proc = setupHandler(cnf);
return true;
}
return {
setup: setupHandler,
process: {
set: setProcess,
options: {
value: myconst
}
}
}
}
我的handler.d.t.ts文件现在看起来像这样。
Handler.D.TS
export = handler;
declare function handler(): any;
我已经尝试了以下内容,但不确定这是否是正确工作的正确方法。有什么建议吗?
目标我试图达到的是:
创建一个.d.ts文件,该文件将能够显示闭合功能的返回对象值/类型(预定)。这主要用于文档和代码Intellisense用途
export = handler;
declare function setupHandler(config: any): any;
declare function setProcess(config: any): any;
declare interface handlerreturn {
setup: typeof setupHandler,
process: {
set: typeof setProcess,
options: {
value: number
}
}
}
declare function handler(): handlerreturn;
是否有更好的处理方法?由于它是闭合函数,因此该函数被保留。
评论更新:
实际文件: https://github.com/cggi-js/ cgi-js/blob/main/src/process.js
配置对象结构如文件process
属性对象所示:此处: https://github.com/cgi-js/ cgi-js/blob/main/src/configs.js
process.d.ts
(替代尝试):
export default handler;
export type setup = () => void;
export type setProcess = (config: any) => void;
export type getProcess = () => void;
export type registerHandlers = () => void;
export type exec = () => void;
export type execFile = () => void;
export type fork = () => void;
export type spawn = () => void;
export type executeProcess = () => void;
export type executeAction = () => void;
export type kill = () => void;
export type handlerreturn = {
set: () => void,
process: {
set: setProcess,
get: getProcess,
registerHandlers: registerHandlers,
exec: exec,
execFile: execFile,
fork: fork,
spawn: spawn,
executeProcess: executeProcess,
executeAction: executeAction,
kill: kill,
}
}
/**
*
* handler
* Process Execution and Management handler
*
*
* @returns { Object } Process module functions
* Module Object ==> { Process Object }
*
* setup [function]
* process [object]: {
* set [function],
* get [function],
* registerHandlers [function],
* exec [function],
* execFile [function],
* fork [function],
* spawn [function],
* executeProcess [function],
* executeAction [function],
* kill [function]
* }
*
*/
declare function handler(): {
set: () => void,
process: {
set: () => void,
get: () => void,
registerHandlers: () => void,
exec: () => void,
execFile: () => void,
fork: () => void,
spawn: () => void,
executeProcess: () => void,
executeAction: () => void,
kill: () => void,
}
};
// Alternate process.d.ts file
// declare function handler(): handlerreturn;
I am declaring the function in my typescript definition file .d.ts. It returns a complex predefined object. Need help with the same.
I used the npx -p typescript tsc src/handler.js --declaration --allowJs --emitDeclarationOnly --outDir types
the kind of command and it gave me a .d.ts file for handler.js to be something like below:
handler.js
function handler() {
const myconst = 10;
function setupHandler (config) {
config.value = myconst;
return { ...config }
}
function setProcess(cnf) {
let proc = setupHandler(cnf);
return true;
}
return {
setup: setupHandler,
process: {
set: setProcess,
options: {
value: myconst
}
}
}
}
My handler.d.ts file generated right now looks like this.
handler.d.ts
export = handler;
declare function handler(): any;
I have tried the following and not sure if this is the right way to get the types being worked right. Any suggestions?
Goal I am trying to reach is:
Create a .d.ts file that will be able to show the return object value/type (predefined) of a closure function. This is mainly for documentation and code intellisense purposes
export = handler;
declare function setupHandler(config: any): any;
declare function setProcess(config: any): any;
declare interface handlerreturn {
setup: typeof setupHandler,
process: {
set: typeof setProcess,
options: {
value: number
}
}
}
declare function handler(): handlerreturn;
Is there any better way of handling this? The function is kept that way since it is a closure function.
UPDATE on comments:
The actual file:
https://github.com/cgi-js/cgi-js/blob/main/src/process.js
The config object structure is as in the file process
attribute object here:
https://github.com/cgi-js/cgi-js/blob/main/src/configs.js
process.d.ts
(alternate try):
export default handler;
export type setup = () => void;
export type setProcess = (config: any) => void;
export type getProcess = () => void;
export type registerHandlers = () => void;
export type exec = () => void;
export type execFile = () => void;
export type fork = () => void;
export type spawn = () => void;
export type executeProcess = () => void;
export type executeAction = () => void;
export type kill = () => void;
export type handlerreturn = {
set: () => void,
process: {
set: setProcess,
get: getProcess,
registerHandlers: registerHandlers,
exec: exec,
execFile: execFile,
fork: fork,
spawn: spawn,
executeProcess: executeProcess,
executeAction: executeAction,
kill: kill,
}
}
/**
*
* handler
* Process Execution and Management handler
*
*
* @returns { Object } Process module functions
* Module Object ==> { Process Object }
*
* setup [function]
* process [object]: {
* set [function],
* get [function],
* registerHandlers [function],
* exec [function],
* execFile [function],
* fork [function],
* spawn [function],
* executeProcess [function],
* executeAction [function],
* kill [function]
* }
*
*/
declare function handler(): {
set: () => void,
process: {
set: () => void,
get: () => void,
registerHandlers: () => void,
exec: () => void,
execFile: () => void,
fork: () => void,
spawn: () => void,
executeProcess: () => void,
executeAction: () => void,
kill: () => void,
}
};
// Alternate process.d.ts file
// declare function handler(): handlerreturn;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
任何
实质上都会击败打字稿的目的 - 类似于根本不进行任何类型检查。除非您绝对必须使用,否则不要使用它,这几乎永远不会。浏览
处理程序
返回的对象...您具有一个函数,该函数获取对象,并添加value
是一个数字。除非您有一个特定的想法,该想法是什么样的对象,要灵活,请使用仿制药,以便返回类型可以与传递的类型一致。新对象是旧对象的克隆,但具有新值。使用:和
setProcess
呼叫setuphandler
,但是setProcess
不使用呼叫的结果,并返回true,看起来像个错误,但是,如果这确实是您拥有的代码,那么它很简单...而
value
只是一个数字。使用这些类型定义
处理程序
的返回对象。您还可以将内联类型放在返回的对象类型中,而不必事先定义它们。
Using
any
essentially defeats the purpose of TypeScript - it's similar to not doing any type-checking at all. Don't use it unless you absolutely have to, which should be almost never.Going through the object that
handler
returns... you have a function that takes an object and adds avalue
which is a number to it. Unless you have a specific idea of what sort of object this is, to be flexible, use generics so that the return type can be in line with the type passed in. You also probably didn't mean to mutate the object, but return a new object which is a clone of the old object, but with the new value. Use:and
setProcess
callssetupHandler
, butsetProcess
doesn't use the result of the call, and returns true, which looks like a mistake, but if that's really the code you have, then it's pretty simple...And the
value
is just a number.Use those types to define the returned object of
handler
.You can also put the types inline in the returned object type instead of defining them beforehand if you wish.