Skip to content
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

Using Generics in an interface, get error TS2322: Type 'string' is not assignable to type 'T'. #10761

Closed
yak77 opened this issue Sep 7, 2016 · 8 comments
Labels
Duplicate An existing issue was already created

Comments

@yak77
Copy link

yak77 commented Sep 7, 2016

I get the above error using react and mobx in the code below. It may be related to #10589, but could also be a separate cause. The code below compiles successfully with version 1.8.10.

TypeScript Version: 2.0.2 & 2.1.0-dev.20160907

Code

// A *self-contained* demonstration of the problem follows...

//input.tsx
interface Props<T> extends React.HTMLProps<Input<T>>
{
    val: IObservableValue<T>;
    onChange?: React.FormEventHandler;
}

interface State
{
}

@observer
export class Input<T> extends React.Component<Props<T>, State> {

    private onChange(e: any): void
    {
        // ....
    }

    render(): JSX.Element {
        // ....
    }
}

// testComponent.tsx
import * as React from "react";
import {observable} from "mobx";
import {observer} from "mobx-react";

import {Input} from "./input";

interface IProps {
}

interface IState {
}

@observer
export class TestComponent extends React.Component<IProps, IState> {
    private _data = observable("");

    constructor(props: IProps, context: any) {
        super(props, context);
    }

    render(): JSX.Element {
        return (
            <div>
                Enter Data: <Input val={this._data} type="text"/><br/>       // <-- Receive TS2322 error on this line when trying to set val={this._data}
            </div>
        );
    }
}


**Expected behavior:**
To Compile

**Actual behavior:** 
Compile Error TS2322
@mhegazy
Copy link
Contributor

mhegazy commented Sep 7, 2016

where is the definition of IObservableValue coming from?

@yak77
Copy link
Author

yak77 commented Sep 8, 2016

It's from MobX type definition:

interface IObservableValue<T> {
    get(): T;
    set(value: T): void;
    intercept(handler: IInterceptor<IValueWillChange<T>>): Lambda;
    observe(listener: (newValue: T, oldValue: T) => void, fireImmediately?: boolean): Lambda;
}

@mhegazy
Copy link
Contributor

mhegazy commented Sep 8, 2016

i can not get this to repro. can you provide a self contained sample i could try?

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Sep 8, 2016
@yak77
Copy link
Author

yak77 commented Sep 8, 2016

I created a sample project that repros the issue. MobX not required, only using react. For some reason it wouldn't let me attach the zip to this issue, so I uploaded it here:
https://dl.dropboxusercontent.com/u/29165199/Test.zip

After extracting the files simply run "npm install" to set up.

Then to build with TypeScript 1.8.10 run: "npm run build-1.8"
Then to build with TypeScript rc (currently 2.0.2) run: "npm run build-rc"

The 1.8.10 build compiles fine, the rc (2.0.2 ) build fails with TS2322.

@yak77
Copy link
Author

yak77 commented Sep 16, 2016

Just wondering if you had a chance to try and pull down the sample project zip file and if you were able to reproduce the issue?

Let me know if you have any other questions. Thanks.

@RyanCavanaugh
Copy link
Member

@yak77 sorry for the delay, looking now

@RyanCavanaugh
Copy link
Member

This is just a duplicate of #6395 -- it's currently not possible to use a generic class as a JSX element class. A workaround is

  render(): JSX.Element
  {
    // Might want to have a generic type alias for this
    const StringInput: new() => Input<string> = Input;
    return (
        <div>
          <StringInput val={this._data}/>
        </div>
    );
  }

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Needs More Info The issue still hasn't been fully clarified labels Sep 16, 2016
@mhegazy mhegazy closed this as completed Sep 20, 2016
@yak77
Copy link
Author

yak77 commented Sep 23, 2016

@RyanCavanaugh Thanks for looking into this and posting the workaround.

Is this a regression in the 2.0 compiler? Because the code I posted compiles with 1.8.10.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants