通过任何方式“推断”类方法参数

时间:2019-12-04 02:18:21

标签: typescript

我有一个类,它有两个方法,看起来像这样:

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 };
}

我正在寻找一种方法:

  1. 确保PlotPrompt具有这两种方法。
  2. 是否可以自动键入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 }}')
    }
}

1 个答案:

答案 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 }}')
    }
}