Typescript:如何获得包含泛型的泛型方法的类型?

时间:2019-07-17 07:37:11

标签: reactjs typescript

我需要提取React.useState的返回类型,因为Typescript不能正确推断出它,所以我认为这很简单:

---
# tasks file for vmware_subnet_comibned
- name: Finding available IP & setting DNS
  SubnetExplorerAnsible:
    hostname: "{{ orion_hostname }}"
    username: "{{ orion_username }}"
    password: "{{ orion_password }}"
    server_name: "{{ orion_server_name}}"
    subnet: "{{ orion_subnet }}"
    dns_server_one: "{{ orion_dns_server_one }}"
    dns_server_two: "{{ orion_dns_server_two }}"
    dns_zone: "{{ orion_dns_zone }}"
    ipaddr: "{{ orion_ipaddr }}"
    subnet_mask: "{{ orion_subnet_mask }}"
  register: result
- debug: var=result

- name: Cloning VMWare Template "{{ template_name }}"
  vmware_guest:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: "{{ validate_certs }}"

    datacenter: "{{ datacenter_name }}"
    cluster: "{{ cluster_name }}"
    template: "{{ template_name }}"
    datastore: "{{ datastore_name }}"
    folder: "{{ folder_name }}"
    name: "{{ orion_server_name }}"
    disk:
    - size_gb: "{{ disk_size_gb }}"
      type: thin
    hardware:
      memory_mb: "{{ memory_size_mb }}"
      num_cpus: "{{ num_cpus }}"
    networks:
    - name: "{{ orion_subnet }}"
      type: static
      ip: "{{ orion_ipaddr }}"
      netmask: "{{ orion_subnet_mask }}"
      gateway: "{{ gateway }}"
      dns_servers:
      - "{{ orion_dns_server_one }}"
      - "{{ orion_dns_server_two }}"
    wait_for_ip_address: yes
    state: poweredon
  delegate_to: localhost
  register: result
- debug: var=result

不幸的是,语法不起作用(您不能将通用类型传递给useState),因此我不得不从源代码中复制类型:

   type UseStateReturnType<S> = ReturnType<typeof React.useState<S>>;

我的问题是,是否可以使用其他Typescript语法做我想做的事?

0 个答案:

没有答案