diff --git a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts index cd2f0b11826b..c05d6c6dab35 100644 --- a/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts +++ b/packages/eslint-plugin/src/util/explicitReturnTypeUtils.ts @@ -52,11 +52,12 @@ function isConstructorArgument( } /** - * Checks if a node belongs to: + * Checks if a node is a property or a nested property of a typed object: * ``` * const x: Foo = { prop: () => {} } * const x = { prop: () => {} } as Foo * const x = { prop: () => {} } + * const x: Foo = { bar: { prop: () => {} } } * ``` */ function isPropertyOfObjectWithType( @@ -82,7 +83,8 @@ function isPropertyOfObjectWithType( isTypeAssertion(parent) || isClassPropertyWithTypeAnnotation(parent) || isVariableDeclaratorWithTypeAnnotation(parent) || - isFunctionArgument(parent) + isFunctionArgument(parent) || + isPropertyOfObjectWithType(parent) ); } diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index 6ce8210822ba..4faf3fa18b70 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -156,6 +156,34 @@ const x = { code: ` const x: Foo = { foo: () => {}, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + // https://github.com/typescript-eslint/typescript-eslint/issues/2864 + { + filename: 'test.ts', + code: ` +const x = { + foo: { bar: () => {} }, +} as Foo; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +const x = { + foo: { bar: () => {} }, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +const x: Foo = { + foo: { bar: () => {} }, }; `, options: [{ allowTypedFunctionExpressions: true }], diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index 3652ebc876ac..e05f4f09230d 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -181,6 +181,34 @@ export const x = { code: ` export const x: Foo = { foo: () => {}, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + // https://github.com/typescript-eslint/typescript-eslint/issues/2864 + { + filename: 'test.ts', + code: ` +export const x = { + foo: { bar: () => {} }, +} as Foo; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +export const x = { + foo: { bar: () => {} }, +}; + `, + options: [{ allowTypedFunctionExpressions: true }], + }, + { + filename: 'test.ts', + code: ` +export const x: Foo = { + foo: { bar: () => {} }, }; `, options: [{ allowTypedFunctionExpressions: true }],