繁体   English   中英

如何修复 typescript 错误类型“string”不可分配给类型“Ref”<inputref> | 不明确的'?</inputref>

[英]How to fix typescript error Type 'string' is not assignable to type 'Ref<InputRef> | undefined'?

为什么编译失败??

C:/Users/danie/OneDrive/Desktop/Project/seller-app/src/components/ui/maps/places-autocomplete.tsx
TypeScript error in C:/Users/danie/OneDrive/Desktop/Project/seller-app/src/components/ui/maps/places-autocomplete.tsx(12,55):
Type 'string' is not assignable to type 'Ref<InputRef> | undefined'.  TS2322

    10 |   )
    11 |   return (
  > 12 |     <Input placeholder='Start typing or move the map' ref='autoCompleteRef' />
       |                                                       ^
    13 |   )
    14 | }
    15 |

此错误发生在构建期间,无法消除。

完整的源代码:

import { Input } from "antd"
import React, { useRef } from "react"

const PlacesAutocomplete = () => {
  const autoCompleteRef = useRef(null)

  const autoComplete = new (window as any).google.maps.places.Autocomplete(
    autoCompleteRef.current,
    { types: ["(cities)"], componentRestrictions: { country: "us" } }
  )
  return (
    <Input placeholder='Start typing or move the map' ref='autoCompleteRef' />
  )
}

export default PlacesAutocomplete

错误信息:

  Type 'string' is not assignable to type 'Ref<InputRef> | undefined'.ts(2322)
  index.d.ts(133, 9): The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & InputProps & RefAttributes<InputRef>'

index.d.ts(133, 9)中配置字符串的源码:

interface RefAttributes<T> extends Attributes {
    ref?: Ref<T> | undefined;
}

ref 应该是 object 而不是字符串。 ref={autoCompleteRef}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM