Skip to content

Mapped type loses type bound when used with arithmetic operator #15501

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue May 1, 2017 · 1 comment
Closed

Mapped type loses type bound when used with arithmetic operator #15501

ghost opened this issue May 1, 2017 · 1 comment
Labels
Bug A bug in TypeScript

Comments

@ghost
Copy link

ghost commented May 1, 2017

TypeScript Version: nightly (2.4.0-dev.20170501)

Code

interface I { x: number; }

type IdentityMap<T> = {
    [P in keyof T]: T[P];
};

function f<T extends I>(i: IdentityMap<T>) {
    const n: number = i.x; // No error, good
    i.x * 2; // Error
}

Expected behavior:

No error.

Actual behavior:

src/a.ts(9,5): error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number' or an enum type.

May be related to #14360.

@mhegazy
Copy link
Contributor

mhegazy commented May 1, 2017

This is not really special to mapped types. this is true for all generic variables:

function f2<T extends number>(x: T) {
    const n: number = x; // No error, good
    x * 2; // Error
}

the check we perform for these operators is that it has to be the type number, and not just a number.

I am sure we have another issue for strings somewhere, but could not find it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

1 participant