Skip to content

Inconsistent function return type vs return type #28634

Closed
@BrendonSled

Description

@BrendonSled

TypeScript Version: 3.3.0-dev.20181121

Search Terms: function types, return types, inconsistent

Code

const test = { 1: '', 2: '', 3: '' };

type keys = { [key in keyof typeof test]: string };
type StateFunction<TState> = () => TState;

const a: StateFunction<keys> = () => ({ ...test, 4: '' }); //Does not error

const b = (): keys => ({ ...test, 4: '' }); //Errors properly

Expected behavior:
const a: StateFunction<keys> = () => ({ ...test, 4: '' }); Should error

Actual behavior:
const a: StateFunction<keys> = () => ({ ...test, 4: '' }); Does not error

Playground Link: http://www.typescriptlang.org/play/#src=const%20test%20%3D%20%7B%201%3A%20''%2C%202%3A%20''%2C%203%3A%20''%20%7D%3B%0D%0A%0D%0Atype%20keys%20%3D%20%7B%20%5Bkey%20in%20keyof%20typeof%20test%5D%3A%20string%20%7D%3B%0D%0Atype%20StateFunction%3CTState%3E%20%3D%20()%20%3D%3E%20TState%3B%0D%0A%0D%0Aconst%20a%3A%20StateFunction%3Ckeys%3E%20%3D%20()%20%3D%3E%20(%7B%20...test%2C%204%3A%20''%20%7D)%3B%0D%0A%0D%0Aconst%20b%20%3D%20()%3A%20keys%20%3D%3E%20(%7B%20...test%2C%204%3A%20''%20%7D)%3B

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDuplicateAn existing issue was already createdFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions