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

proposal: "is" keyword #1289

Closed
alidorosty1234 opened this issue Nov 27, 2014 · 4 comments
Closed

proposal: "is" keyword #1289

alidorosty1234 opened this issue Nov 27, 2014 · 4 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Out of Scope This idea sits outside of the TypeScript language design constraints

Comments

@alidorosty1234
Copy link

I'd like to suggest a language feature. The "is" keyword would check if an object is of a specific type (using duck typing).

interface myInterface {
    myVar1: number;
    myVar2: number;
}
var a;
console.log(a is myInterface);

translates into:

var a;
console.log(typeof(a.myVar1) != "undefined" && typeof(a.myVar2) != "undefined");
@fdecampredon
Copy link

In my opinion this is very hard to implement feature, and I doubt it's feasible.

Firstly it has been discussed in ECMAProposal about an is operator that would have the same behavior than Object.is so TypeScript should really not introduce an operator with this name in case it lands in ES7.
Also I doubt it's feasible. firstly in your example what if a was { myVar1: 'hello'; myVar2: 'world' } your check with 'undefined' would not work. also what about that kind of interface :

interface MyIface {
  new (): something;
  (a: stringng): somethingElse
}

Or very complex interface with union types etc etc.
The generated type check would take hundred of lines of code and could completely kill performance (especially if used in a loop).

@RyanCavanaugh RyanCavanaugh added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Out of Scope This idea sits outside of the TypeScript language design constraints labels Dec 1, 2014
@RyanCavanaugh
Copy link
Member

fdecampredon nailed it -- this varies from "way too much code" to "simply impossible". TypeScript intentionally avoids adding runtime type information overhead.

@danquirk
Copy link
Member

danquirk commented Dec 1, 2014

Note that we would be able to do something like this for classes with instanceof and type guards (#1283)

@sophiajt sophiajt closed this as completed Dec 3, 2014
@jbondc
Copy link
Contributor

jbondc commented Jun 2, 2015

Could be supported via a user-defined 'is function':

interface myInterface {
    myVar1: number;
    myVar2: number;
} is<T> => T.myVar1 !== undefined && T.myVar2 !== undefined
var a;
console.log(a is myInterface);

Emits

var a;
console.log(a.myVar1 !== undefined && a.myVar2 !== undefined);

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Out of Scope This idea sits outside of the TypeScript language design constraints
Projects
None yet
Development

No branches or pull requests

6 participants