简体   繁体   English

检查打字稿中的默认值

[英]Checking for default values in Typescript

Is there any way in typescript / typescript-eslint to render an error when an optional parameter does not have aa default value? 当可选参数没有默认值时,typescript / typescript-eslint中有什么方法可以呈现错误? I am trying to convert my React codebase from JSX to TSX and no longer having the warnings about not having defaultProps defined is worrisome. 我试图将我的React代码库从JSX转换为TSX,并且不再有关于未定义defaultProps的警告,这令人担忧。 Thanks. 谢谢。

bad: title does not have default prop value 错误:标题没有默认的道具值

import * as React from 'react';

interface Props {
  title?: string;
}

const SampleComponent: React.FC<Props> = ({ title }) => (
  <h1>
    {title && <p>{title}</p>}
  </h1>
);

export default SampleComponent;

good: title has default prop value 良好:标题具有默认的道具价值

import * as React from 'react';

interface Props {
  title?: string;
}

const SampleComponent: React.FC<Props> = ({ title = 'foo' }) => (
  <h1>
    {title && <p>{title}</p>}
  </h1>
);

export default SampleComponent;

This isn't something TypeScript will do for you, so there's no reliable & easy option available. 这不是TypeScript可以为您完成的,因此没有可靠且简单的选项。

However, with a little work it is something that could be implemented as an ESLint rule. 但是,只需做一些工作,就可以将其实现为ESLint规则。 Linting rules are given the abstract syntax tree of your code (AST - a data structure describing the code of a program), and can then run checks against it, such as getting every parameter, filtering to just the optional parameters, and then checking if those all have a default value. 为规则规则提供了代码的抽象语法树(AST-描述程序代码的数据结构),然后可以对其进行检查,例如获取每个参数,仅过滤至可选参数,然后检查是否这些都具有默认值。

To actually do this, I would suggest: 为了做到这一点,我建议:

  • Set up ESLint with the ESLint-TypeScript plugin in your project 在项目中使用ESLint-TypeScript插件设置ESLint
  • Read this introduction to writing a custom ESLint rule: https://blog.yonatan.dev/writing-a-custom-eslint-rule-to-spot-undeclared-props/ (note that this is looking at the JavaScript AST, not TypeScript, but it's very similar, and pure JS is a good starting point) 阅读此介绍以编写自定义ESLint规则: https ://blog.yonatan.dev/writing-a-custom-eslint-rule-to-spot-undeclared-props/(请注意,这是在查看JavaScript AST,而不是JavaScript AST) TypeScript,但是非常相似,纯JS是一个很好的起点)
  • Take a look at simple existing TS linting rules like no-parameter-properties (no private / public /etc properties on constructor arguments), and make sure you understand how they work 看一下简单的现有TS插入规则,例如no-parameter-properties (构造函数参数上没有private / public / etc属性),并确保您了解它们如何工作
  • Have a go at writing your own 可以自己写

Note that tslint also exists, as a purely TypeScript-focused linting tool. 请注意,tslint也存在,它是纯粹以TypeScript为重点的整理工具。 This may be an option, and historically this has been more popular for TS linting, but it's now deprecated in favour of eslint-typescript, so I would avoid starting with it nowadays. 这可能是一个选择,并且从历史上看,这在TS linting上更受欢迎,但是现在不推荐使用 eslint打字稿,因此,我今天避免从它开始。

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

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