Skip to content

Unable to add numeric types that are not simply 'number' #17069

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
DanielRosenwasser opened this issue Jul 10, 2017 · 4 comments · Fixed by #17870 or #17912
Closed

Unable to add numeric types that are not simply 'number' #17069

DanielRosenwasser opened this issue Jul 10, 2017 · 4 comments · Fixed by #17870 or #17912
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 10, 2017

I'm trying to rewrite the signature of sum from core.ts in our compiler (see #16823), but I'm getting this error:

export function sum<T extends Record<K, number>, K extends string>(array: T[], prop: K): number {
    let result = 0;
    for (const v of array) {
        result += v[prop];
     // ~~~~~~~~~~~~~~~~~
     // Operator '+=' cannot be applied to types 'number' and 'T[K]'.
    }
    return result;
}

Note that I can't declare sum as

export function sum<K extends string>(array: Record<K, number>[], prop: K): number

because then TypeScript would infer all of the properties in each element for K. So I do need another type argument T for the indirection.

For a simpler repro:

function foo<T extends number>(x: T) {
    return 0 + x;
    //     ~~~~~
    // Operator '+' cannot be applied to types '0' and 'T'.

}

Related is #15645.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Jul 10, 2017
@DanielRosenwasser DanielRosenwasser changed the title Unable to add types constrained to 'number' Unable to add types that are not simply 'number' Jul 10, 2017
@DanielRosenwasser DanielRosenwasser changed the title Unable to add types that are not simply 'number' Unable to add numeric types that are not simply 'number' Jul 10, 2017
@ghost
Copy link

ghost commented Jul 10, 2017

Related: #15501

sandersn added a commit that referenced this issue Aug 17, 2017
1. `T[K]` now correctly produces `number` when
   `K extends string, T extends Record<K, number>`.
2. `T[K]` no longer allows any type to be assigned to it when
   `T extends object, K extends keyof T`.

Previously both of these cases failed in
getConstraintOfIndexedAccessType because both bases followed `K`'s base
constraint to `string` and then incorrectly produced `any` for types
(like `object`) with no string index signature. In (1), this produced an
error in checkBinaryLikeExpression`. In (2), this failed to produce an
error in `checkTypeRelatedTo`.
@sandersn
Copy link
Member

Fix is up at #17870

@sandersn sandersn reopened this Aug 17, 2017
@sandersn
Copy link
Member

Note that #17404 fixed the simpler repro; #17870 fixes the original issue with a type parameter indexed by another type parameter.

@sandersn sandersn added the Fixed A PR has been merged for this issue label Aug 17, 2017
@sandersn sandersn reopened this Aug 18, 2017
sandersn added a commit that referenced this issue Aug 18, 2017
1. `T[K]` now correctly produces `number` when
   `K extends string, T extends Record<K, number>`.

2. `T[K]` no longer allows any type to be assigned to it when
   `T extends object, K extends keyof T`.

Previously both of these cases failed in
getConstraintOfIndexedAccessType because both bases followed `K`'s base
constraint to `string` and then incorrectly produced `any` for types
(like `object`) with no string index signature. In (1), this produced an
error in checkBinaryLikeExpression`. In (2), this failed to produce an
error in `checkTypeRelatedTo`.
@sandersn
Copy link
Member

The previous PR got merged by mistake and is now reverted; #17912 is the PR with the fix now.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
3 participants