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

Capability generate User-Defined Type Guards (enable basic TypeCheck at Runtime) #27

Closed
CanKattwinkel opened this issue Jun 20, 2017 · 6 comments
Milestone

Comments

@CanKattwinkel
Copy link

Since we are working with plain objects in JavaScript and not with class-instances we can't do typeof checks. TypeScript has a feature called Type Guards that can help you with that.

Example:


class Person {
  name: string;
  age: number;
  mail: string;
  optionalVal?: any;
}

const person: Person = {
  name: 'Can',
  age: 6,
  mail: 'can@mail.mail'
};

const notAPerson =  {
  name: 'Can',
  mail: 'can@mail.mail'
};
console.log(typeof person, typeof person === typeof Person); // --> 'object', false :(



// with a  'typeguard' function 
function instanceOfPerson(object: any): object is Person {
  return 'name' in object
      && 'age' in object
      && 'mail' in object;
}

// usage:
console.log(typeof person, instanceOfPerson(person)); // --> 'object', true :)
console.log(typeof person, instanceOfPerson(notAPerson)); // --> 'object', false :)

https://codepen.io/anon/pen/weJbEr

So it'd be nice to have a setting || annotation to enable the generation for instanceOfX functions.

User-Defined Type Guards - TypeScript Docs

TypeScript Deep Dive

@dzuvic dzuvic added this to the 0.3.0 milestone Jun 20, 2017
@dzuvic
Copy link
Owner

dzuvic commented Jun 20, 2017

Thank you. I hope it will be implemented in the 0.3.x milestone.

@pburgmer
Copy link

Proposal: implement as static method on Person class. So we can call Person.isInstance(object)

@dzuvic dzuvic removed the easy label Jul 17, 2017
@dzuvic
Copy link
Owner

dzuvic commented Jul 18, 2017

Note: Classes will be Implemented with #31 . As far as i know, interfaces don't take concrete implementations. Meaning: functions with this tickets and class members in #31

@dzuvic
Copy link
Owner

dzuvic commented Sep 9, 2017

There are several issues to be resolved:

  • The JSON mapper might exclude some fields, so the type guard should be somehow configurable. this is out of scope for 0.3.0
  • it's hard to detect a optional type in jtsgen and is not supported. Even if you define a mapping like abc -> abc? This is out of scope for 0.3.0, also
  • The fileds of the super types have to be included.

@dzuvic
Copy link
Owner

dzuvic commented Sep 17, 2017

First try using undefined:

  export interface InterFaceTestTypeGuard1 {
    typeGuard1: string;
  }


  export function instanceOfInterFaceTestTypeGuard1(x: any): x is InterFaceTestTypeGuard1{
    return x &&
      x.typeGuard1 !== undefined;
  }

@CanKattwinkel is this the right way?

@dzuvic
Copy link
Owner

dzuvic commented Oct 30, 2017

added handling of supertypes: IMHO this feature is ready to be tested.

@dzuvic dzuvic closed this as completed Oct 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants