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

Type guards #42

Closed
Ithalica opened this issue Aug 2, 2017 · 2 comments
Closed

Type guards #42

Ithalica opened this issue Aug 2, 2017 · 2 comments

Comments

@Ithalica
Copy link

Ithalica commented Aug 2, 2017

Is it possible to write C# code to generate TypeScript type guards?

My case if the following:

export interface IBaseClass
{
	Id: number;
	Name: string;
}
export interface IFirstClass extends IBaseClass
{
	SomeProperty: number;
}
export interface ISecondClass extends IBaseClass
{
            SomeOtherProperty: string;
}

and wanting to use it in a method like

    doStuff(value: IBaseClass): void
    {
            if (value is IFirstClass) {
                    //Do something with value.SomeProperty 
            }
    }

I know that in order to do this, I need some type guarding code like this:

export function isFirstClass(item: IFirstClass | ISecondClass): item is IFirstClass {
    return (<IFirstClass>item).SomeProperty !== undefined;
}

Writing it, is not a big deal as such, but I wanted to know if there are any built in options to generate this so I don't have to maintain it manually when the file is updated?

@pavel-b-novikov
Copy link
Member

pavel-b-novikov commented Aug 2, 2017

Currently this feature is not supported

@pavel-b-novikov
Copy link
Member

Well, I have re-read your request and can suggest to use such tools as Substitutions and inline type inferers

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

No branches or pull requests

2 participants