Skip to content

'in' should not operate on primitive types #41317

Closed
@millsp

Description

@millsp

TypeScript Version: 4.1.0-dev

Search Terms:
operator in exception crash unhandled

Code

const hasKey = <A, K extends string | number | symbol>(
    thing: A,
    key: K,
): boolean => {
    return key in thing;
};

hasKey(123, 'hello'); // TypeError exception

Expected behavior:

TS should expect A to be an object

Actual behavior:

TS didn't detect the potential crash

Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-beta#code/MYewdgzgLgBAFgQwgaQKYE8YF4YB4CCANDMjKgB5SpgAmEM0ATgJZgDmMAPjGAK4C2AI1SMuDdEJAAbAHwAKAFAxlMKHFZsAXDCJKVAawzbkhBQEptgkNNQIw2GTADee5Y1RReje4cytV6uwA3AoAviEKiCgYcgCMAEwAzMQA5HCoUlIgKWZBMAD0+TAAKugADqgAooyMIKIUwKhlUMzgCkA

Related Issues:

Metadata

Metadata

Assignees

Labels

Breaking ChangeWould introduce errors in existing codeCommittedThe team has roadmapped this issueFix AvailableA PR has been opened for this issueGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do thisSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions