简体   繁体   中英

How do I get the type of a generic parameter in Typescript?

I have this Flux Store class:

'use strict';
import flux = require('app/tools/flux');
import types = require('app/tools/types');
import Actions = require('app/actions/actions');

class Store
{
    bindListeners(config : any) : void {;};
    books : Array<types.IBook>;
    selectedBookName : string;

...
}

export = flux.createStore<Store>(Store, 'Store');

That's being used in this view:

"use strict";
import React = require('react');
import Store = require('app/stores/store'); // <-- here we import the Store
import _ = require('lodash');
import BookHelper = require('app/tools/bookHelper');
import Msg = require('app/tools/messages');

interface props {}
interface state {}

class NoteContainer extends React.Component<props, state>
{
    state: typeof Store; // <-- this is Altjs<Store>, not Store :(

    render()
    {
        if (!this.state.selectedBookName)  // <-- here's an error
            return;
...

which gives this compiles error:

error TS2339: Property 'selectedBookName' does not exist on type 'AltStore<Store>'.

How do I set the state of the view to be the actual Store class, not the AltStore<Store> class? Ie how can I get the type of the generic parameter, like this: state: typeof Store<THIS THING>

The state parameter is just a type parameter for the compiler. It is not available to you at runtime, it's just there so that methods like setState can be type-checked. From your code, though, you are exporting an instance of the store rather than the Store type itself. So you want to assign that to the state property in the constructor. Also note that type of state needs to be a plain JS object - React will use Object.assign on the state object at runtime, and it will cause problems if it isn't a plain object. So like so:

import storeInstance = require('app/stores/store');

interface StateType {
    store: AltStore<Store>;
}
class NoteContainer extends React.Component<PropsType, StateType>
{
    constructor(props: PropsType) {
        super(props);
        this.state = {store: storeInstance};
    }

Also note that you don't need to specify the type of the state property, the type parameters in the Component class definition takes care of that for you.

I'm not 100% sure about exporting an instance from a module like you're doing here, I haven't been able to get that to work, but I haven't used the require-style imports. If it doesn't work, you may need to wrap it in a function that returns the instance and export the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM