简体   繁体   English

为什么 typescript 不警告这个 function 中的返回类型?

[英]Why doesn't typescript warn about the return type in this function?

For example, I have an onClick handler on a <div> tag.例如,我在<div>标记上有一个 onClick 处理程序。 The handler should expect a return type of React.MouseEventHandler<HTMLDivElement> | undefined处理程序应该期望返回类型为React.MouseEventHandler<HTMLDivElement> | undefined React.MouseEventHandler<HTMLDivElement> | undefined . React.MouseEventHandler<HTMLDivElement> | undefined But, I still can return a boolean value of false when temp = false .但是,当temp = false时,我仍然可以返回 false 的 boolean 值。 Got confused here why Typescript did not warn me about the return type在这里感到困惑为什么 Typescript 没有警告我有关返回类型的信息

import React from 'react';

const Dummy= () => {
  const [temp, settemp] = React.useState(false)
  const [text, settext] = React.useState('Hi')

  const handler : React.MouseEventHandler<HTMLDivElement> | undefined  = () =>{
    return temp && settext('bye')
    //return temp
    
  }
  return (
   <div onClick={handler}> Testing </div>
  );
};

export default Dummy;

You're telling typescript that the handler variable is React.MouseEventHandler<HTMLDivElement> | undefined您告诉 typescript handler变量是React.MouseEventHandler<HTMLDivElement> | undefined React.MouseEventHandler<HTMLDivElement> | undefined . React.MouseEventHandler<HTMLDivElement> | undefined Note that it is never actually undefined, as it is a const and is therefore only ever a function. You're not telling it what the function's return type is.请注意,它实际上从来都不是未定义的,因为它是一个常量,因此永远只是一个 function。您没有告诉它函数的返回类型是什么。 These are two different things.这是两个不同的东西。 The return type is either derived from what you return in the function, or you can explicitly declare it after the parameters, like this:返回类型要么派生自您在 function 中返回的内容,要么您可以在参数之后显式声明它,如下所示:

let sum = (x: number, y: number): number => {
    return x + y;
}

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

相关问题 使用 typescript 定义文件 (.d.ts) 键入 javascript 代码不会警告原始类型错误 - using typescript definition file (.d.ts) for typing javascript code doesn't warn about primitive type errors 为什么 Typescript 不要求我的 function 返回某个类型? - Why isn't Typescript requiring my function to return a certain type? 打字稿 | 关于缺少函数返回类型的警告,ESLint - Typescript | Warning about Missing Return Type of function, ESLint 我不知道打字稿中的返回类型 - I don't know about return type in typescript 为什么接口中的 Typescript 返回类型 void 在实现中不会触发错误? - Why Typescript return type void in interface doesn't trigger error in implementation? 当 typeof 在 function 上运行时,它返回“function”作为类型。 为什么它不返回“对象”? - When typeof is run on a function, it returns “function” as the type. Why doesn't it return “object”? 为什么JSHint会警告我有关在回调函数中使用&#39;this&#39;的信息? - Why does JSHint warn me about using 'this' in a callback function? 为什么 return 不会中断循环和 function? - Why return doesn’t break the loop and function? 为什么函数不返回值? - Why doesn't the function return the value? 打字稿:为什么内部函数不能解析外部函数的参数? - Typescript: Why doesn't inner function resolve the arguments on outer function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM