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

Are there type definitions for TypeScript for *.funcs.ts files #23

Open
SoftHai opened this issue Mar 28, 2020 · 5 comments
Open

Are there type definitions for TypeScript for *.funcs.ts files #23

SoftHai opened this issue Mar 28, 2020 · 5 comments
Labels
documentation Improvements or additions to documentation

Comments

@SoftHai
Copy link

SoftHai commented Mar 28, 2020

Hi,

creating a function with TypeScript will end in some type check "errors" for the parameters:
image

Are there TS type definitions for at least a few of the parameters (like dispatch, history, ...). Option and StateOfDispatch are types which has to be defined in the specific application, isn't it?

Actual you have to define the type definition by your own:
image

Or use a // @ts-ignore to ignore the errors.

@ipselon
Copy link
Contributor

ipselon commented Mar 29, 2020

@SoftHai, thank you for the detailed explanation. I'll add some TS typings into the react-app-framework package in the next release.

As a workaround, I'd like to suggest using the following example:

Install @types/history before: yarn add @types/history -D -E

import { History } from 'history';
import { PrimaryButtonProps } from './PrimaryButton.comp';

type SetTextStateOptions = {
    stateByDispatch: PrimaryButtonProps;
    history: History;
};

// Specify this in the global index.d.ts
type DispatchFunction = (arg0: any) => void;

export const setText = (text: string, stateOptions: SetTextStateOptions) => (dispatch: DispatchFunction) => {
    const { stateByDispatch, history } = stateOptions;
    // ...
    
    // OR
    // const hist: History = stateOptions.history;
    // const stateByDispatch: PrimaryButtonProps = stateOptions.stateByDispatch;
    //...
};

@ipselon
Copy link
Contributor

ipselon commented Mar 29, 2020

@SoftHai,

Option and StateOfDispatch are types which has to be defined in the specific application, isn't it?

Yes, that's correct.

@SoftHai
Copy link
Author

SoftHai commented Mar 29, 2020

Hi, cool.
For the meanwhile your workaround will work.
Feel free to close the issue or keep it open as reminder. As you like.
Thanks

@ipselon
Copy link
Contributor

ipselon commented Mar 29, 2020

@SoftHai, thank you. I'll keep this issue until I add this as an example to the User Guide.

@ipselon ipselon added the documentation Improvements or additions to documentation label Mar 29, 2020
@ipselon
Copy link
Contributor

ipselon commented Mar 31, 2020

@SoftHai,

It seems that my example is not correct. Please consider my following example for TS types in function.

global.d.ts

import { History } from 'history';

export type DispatchFunction<T> = (arg0: T) => void;

export interface StateOptions<T> {
    stateByDispatch: T;
    history: History;
}

functionExamples.funcs.ts

import { StateOptions, DispatchFunction } from "typings/global";
import { PrimaryButtonProps } from './PrimaryButton.comp';

interface StateByDispatch {
    props?: PrimaryButtonProps;
}

interface DispatchOptions extends StateByDispatch {
    // here we already have output fields from StateByDispatch
    // add additional fields for the dispatch output
}

export const setText = (textValue: string, stateOptions: StateOptions<StateByDispatch>) => (dispatch: DispatchFunction<DispatchOptions>) => {
    const { stateByDispatch, history } = stateOptions;
    if (stateByDispatch) {
        const {props} = stateByDispatch;
        dispatch({props: {...props, text: textValue}});
    }
};

How it is used on the flow:

Screenshot 2020-03-31 at 11 32 25

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants