我有一个类,它有两个方法,看起来像这样:
export class ResolverType {
prompt = () => ({
type: 'list',
name: 'resolverType',
message: 'Choose a resolver type:',
choices: [
'query',
'mutation'
]
})
partials = (plop) => {
plop.setPartial('resolverType', '{{ resolverType }}')
}
}
这是称为PlopPrompt
的普通类的一部分。
每个PlopPrompt
具有相同的形状。他们看起来像这样:
import { NodePlopAPI, PlopGenerator } from 'plop';
abstract class PlopPrompt {
abstract prompt(): PlopGenerator['prompts'][number]
abstract partials(plop: NodePlopAPI): void
}
或
interface PlopPrompt {
prompt(): PlopGenerator['prompts'][number]
partials(plop: NodePlopAPI): void
}
或切换到static
并使用@PlopPrompt
装饰器。
interface PlopPromptInterface {
prompt(): any
partials(plop: NodePlopAPI): void
}
export function PlopPrompt() {
return <U extends PlopPromptInterface>(constructor: U) => { constructor };
}
我正在寻找一种方法:
PlotPrompt
具有这两种方法。partials
自变量?它已经在定义中定义了,我不明白为什么我看不到键入内容,并且plop
参数又返回为any
。不太了解为什么在这里键入plop
:
interface Contract {
prompt(): any
partials(plop: NodePlopAPI): void
}
const example: Contract = {
prompt: () => ({
type: 'list',
name: 'resolverType',
message: 'Choose a resolver type:',
choices: [
'query',
'mutation'
]
}),
partials: (plop) => {
plop.setGenerator()
plop.setPartial('resolverType', '{{ resolverType }}')
}
}
但不能在此处输入
interface Contract {
prompt(): any
partials(plop: NodePlopAPI): void
}
class Example extends Contract {
prompt = () => ({
type: 'list',
name: 'resolverType',
message: 'Choose a resolver type:',
choices: [
'query',
'mutation'
]
})
partials = (plop) => {
plop.setGenerator()
plop.setPartial('resolverType', '{{ resolverType }}')
}
}
有什么办法可以纠正这个问题? I found a long conversation here, but no answers.`
也使用匿名类尝试了此操作。参数仍然没有输入。
const ResolverType: PlopPrompt = class {
static prompt = () => ({
type: 'list',
name: 'resolverType',
message: 'Choose a resolver type:',
choices: [
'query',
'mutation'
]
})
static partials = (plop) => {
plop.setPartial('resolverType', '{{ resolverType }}')
}
}
答案 0 :(得分:0)
这是满足上述要求的唯一方法,但是ResolverType
不再是一个类。
我想知道我可以成为一个班级。
import { NodePlopAPI } from 'plop';
import { PlopPrompt } from '../shared/PlopPrompt';
interface PlopPrompt {
prompt(): any
partials(plop: NodePlopAPI): void
}
export const ResolverType: PlopPrompt = {
prompt: () => ({
type: 'list',
name: 'resolverType',
message: 'Choose a resolver type:',
choices: [
'query',
'mutation'
]
}),
partials(plop) {
plop.setPartial('resolverType', '{{ resolverType }}')
}
}