简体   繁体   English

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

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

Why did it Fail to compile??为什么编译失败??

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 |

This error occurred during the build time and cannot be dismissed.此错误发生在构建期间,无法消除。

The complete source code:完整的源代码:

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

The error message:错误信息:

  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>'

The source code where the string is configured in index.d.ts(133, 9): index.d.ts(133, 9)中配置字符串的源码:

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

the ref should be an object and don't a string. ref 应该是 object 而不是字符串。 ref={autoCompleteRef} ref={autoCompleteRef}

暂无
暂无

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

相关问题 TypeScript(Nodejs 错误) 输入 'string | undefined' 不可分配给类型 'string'。 类型“undefined”不可分配给类型“string” - TypeScript(Nodejs error) Type 'string | undefined' is not assignable to type 'string'. Type 'undefined' is not assignable to type 'string VSCode 如何修复 typescript 突出显示错误 {} 类型的参数不可分配给 - VSCode how to fix typescript highlight error Argument of type {} is not assignable to parameter of TypeScript - 类型未定义不能分配给类型 ICustomType - TypeScript - Type undefined is not assignable to type ICustomType TypeScript:未定义不可分配给类型“布尔|” 连接选项 | 不明确的 - TypeScript: undefined is not assignable to type 'boolean | ConnectionOptions | undefined TypeScript 类型'字符串 | null' 不可分配给类型 'string' - TypeScript Type 'string | null' is not assignable to type 'string' 类型“undefined”不可分配给类型“string | 细绳[]' - Type 'undefined' is not assignable to type 'string | string[]' 打字稿类型&#39;数字&#39;不可分配│键入&#39;字符串&#39; - Typescript Type 'number' is not assignable │ to type 'string' 打字稿:类型“ null”不可分配给类型错误 - Typescript: Type 'null' is not assignable to type error TypeScript:'编号| undefined' 不可分配给“number”类型的参数 - TypeScript: 'number | undefined' is not assignable to parameter of type 'number' 类型 'string' 不可分配给类型 'keyof FitEnum | 不明确的' - Type 'string' is not assignable to type 'keyof FitEnum | undefined'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM