From 47ba6e84f8195a0af8c279698cd63cbadd50998e Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Fri, 28 Feb 2020 00:03:16 -0800 Subject: [PATCH] docs: update docs to match new functionality --- .../docs/rules/no-unsafe-return.md | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/no-unsafe-return.md b/packages/eslint-plugin/docs/rules/no-unsafe-return.md index 46a51be581c4..507abc3dfa28 100644 --- a/packages/eslint-plugin/docs/rules/no-unsafe-return.md +++ b/packages/eslint-plugin/docs/rules/no-unsafe-return.md @@ -1,12 +1,12 @@ # Disallows returning any from a function (`no-unsafe-return`) Despite your best intentions, the `any` type can sometimes leak into your codebase. -Member access on `any` typed variables is not checked at all by TypeScript, so it creates a potential safety hole, and source of bugs in your codebase. +Returned `any` typed values not checked at all by TypeScript, so it creates a potential safety hole, and source of bugs in your codebase. ## Rule Details This rule disallows returning `any` or `any[]` from a function. -This rule also warns against returning `any[]` to help prevent the potential safety hole of returning `any[]` when you have an explicit return type that is an array. +This rule also compares the return type to the function's declared/inferred return type to ensure you don't return an unsafe `any` in a generic position to a receiver that's expecting a specific type. For example, it will error if you return `Set` from a function declared as returning `Set`. Examples of **incorrect** code for this rule: @@ -40,6 +40,13 @@ const foo9 = () => { const foo10 = () => [] as any[]; const foo11 = (): string[] => [1, 2, 3] as any[]; + +// generic position examples +function assignability1(): Set { + return new Set([1]); +} +type TAssign = () => Set; +const assignability2: TAssign = () => new Set([true]); ``` Examples of **correct** code for this rule: @@ -54,6 +61,12 @@ function foo2() { const foo3 = () => []; const foo4 = () => ['a']; + +function assignability1(): Set { + return new Set(['foo']); +} +type TAssign = () => Set; +const assignability2: TAssign = () => new Set(['foo']); ``` ## Related to