Repository: TypeScript
Issue: Constrained generic types infer `never` on a function, even when the argument meets the constraints
Labels: Needs Investigation

# Bug Report

### 🔎 Search Terms

* inference of generic types fails with typed argument
* infer generic never
* constrained generic infers never

### 🕗 Version & Regression Information

- This is the behavior in every version I tried, and I reviewed the FAQ for entries about 4.3.2

### ⏯ Playground Link

[Playground link with relevant code](https://www.typescriptlang.org/dev/bug-workbench/?#code/C4TwDgpgBAaghgGwJYBMDSERQLxQM7ABOSAdgOZQA+UJArgLYBGEhV+ITA9ggNwBQfAMYI4ePFAAqEAgB4JAGigBJKBAAewCCRTj4ydJigAyKAGtMnAGaSAfFADefKM6ioACoU5gAXMv5OXQU4SAkJaQWBOQgAKd08fZQBKBwCXF2AACyQ8ADo4rxxXFA8vfjSAXz5KvlJNQks4QWgAdSRMpRQ5RRV1TW1dRFQMLBNzECtbFLT8hKV+atBIZQAROEYECABREmA2kBkejS0dWEGDEDtcACUIIMJOpUU9Icwbf2FRcSkCACFaJAQKBYclUR36UBud06z3OijgJAudkc0weoL6Jxhw2MZgs1gkNlixXiviUiV8mWy2Na7U6CmUSNSaSghAgwFohBIUyZ3JyvIpeHkjO5zgARDMRb4ZkKXOUyjKqgJLLQSBEkMEoJpfv9AcD8dEyZJpMA-gCgYQ5Ay0iy2RyaBAAO6GrWm3UExLzRXK1XqxjazqMiRo47iJSrdZbHZ7A42QVpQ7ogb6LGjXG2PjRX0uwi+b7Gv2uqltDIdLr0g250tKS0ua3szkkB1O4CVgmZnWEPJErzuhU1HYsBpNFZrDZc5yoXyhUhkD1CYIEKCWTicHNGk3tg5hjZ2EzU4sPLcQRQAclQx8uGrX+fNoZHEDdneip5Qx57QRCwCgjDgrFwbZQ0RLpwPYAPQAFR8AAgoQZAMFon4TIs0DHrm65mpud47lAe4lre4aKGKKAijYx6uOIJCcJ+nxIGQJB3hqnBQGAP5wPQrIsFAiHgMhqHXhh4bbLsoAyA2ABuLA2FhOEHneglRmJEmKAphAkTkAQSNxUAoVeWb8duhY0npR5QIRxGkZSFFUWINF0eGDEappx7SUZcnCcpMZ2uJKnHmpaQaZA4gTGA8QsKAWkzKRP7QKQQT0Mxuzhr5TL+chU7kOZ5GUVA1G0fRkQOUsx7KT5wB4NEABMADMAAsACsiR8GBIF8EAA)

### 💻 Code

<!-- Please post the relevant code sample here as well-->
```ts
type ValidKey = string | number | symbol;

/* The class ultimately being built */
class Test<T, I extends ValidKey & keyof T> {
    idProp: I;

    constructor(idProp: I) {
        this.idProp = idProp;
    }
}

interface WithId<T, I extends ValidKey & keyof T> {
    idProp: I;
}

type IDableEntity<I extends ValidKey> = Record<I, ValidKey>;

class TestBuilder<T extends Record<ValidKey, any>> {
    id<I extends ValidKey & keyof T>(idProp: I): this & WithId<T, I> {
        return {
            ...this,
            "idProp": idProp
        };
    }
}

function testBuilder<T>(): TestBuilder<T> {
    return new TestBuilder<T>();
}

function build<
    T extends IDableEntity<I>,
    I extends ValidKey & keyof T>
(builder: TestBuilder<T> & WithId<T, I>): Test<T, I> {
    return new Test<T, I>(builder.idProp);
}

interface IDable {
    id: string;
}

const foo: TestBuilder<IDable> & WithId<IDable, 'id'> = testBuilder<IDable>().id('id');
const bar = build(foo);
/*
Argument of type 'TestBuilder<IDable> & WithId<IDable, "id">' is not assignable to parameter of type 'TestBuilder<IDableEntity<never>> & WithId<IDableEntity<never>, never>'.
  Type 'TestBuilder<IDable> & WithId<IDable, "id">' is not assignable to type 'WithId<IDableEntity<never>, never>'.
    Types of property 'idProp' are incompatible.
      Type 'string' is not assignable to type 'never'.ts(2345)
*/
```

This also fails if I create a new `type CompleteTestBuilder<T extends IDableEntity<I>, I extends ValidKey & keyof T> = TestBuilder<T> & WithId<T, I>` that `build` accepts as an argument, or if I define `CompleteTestBuilder` as its own `interface extends TestBuilder<T> { idProp: I}`.

A simpler version that does NOT fail:
```ts
function build2<T>(builder: TestBuilder<T>): TestBuilder<T> {
    return builder;
}

const baz: TestBuilder<IDable> = testBuilder<IDable>();
const qux = build2(baz); // qux: TestBuilder<IDable>
```

This version also does NOT fail:
```ts
function build3<T extends Record<ValidKey, any>>(builder: TestBuilder<T>): (t: T) => string {
    return (t: T) => 'boo';
}

const quux = build3(baz); // quux: (t: IDable) => string
```

### 🙁 Actual behavior

The `build` call fails with the error message:
```
Argument of type 'TestBuilder<IDable> & WithId<IDable, "id">' is not assignable to parameter of type 'TestBuilder<IDableEntity<never>> & WithId<IDableEntity<never>, never>'.
  Type 'TestBuilder<IDable> & WithId<IDable, "id">' is not assignable to type 'WithId<IDableEntity<never>, never>'.
    Types of property 'idProp' are incompatible.
      Type 'string' is not assignable to type 'never'.ts(2345)
```

### 🙂 Expected behavior

I would expect the `build` call to succeed, and types `T` and `I` to be inferred as `IDable` and `"id"`, respectively. The argument type is already known and correct (it is even inferred correctly - the type assignment for `foo` is just to show that the type is correct). The types on the function should be able to match.