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

Use instanceof instead of is in type guard function syntax #3437

Closed
mihailik opened this issue Jun 9, 2015 · 2 comments
Closed

Use instanceof instead of is in type guard function syntax #3437

mihailik opened this issue Jun 9, 2015 · 2 comments
Labels
Suggestion An idea for TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@mihailik
Copy link
Contributor

mihailik commented Jun 9, 2015

The type guard syntax introduces a new keyword is
#1007 Support user-defined type guard functions
#3262 Custom type guard function

(the latter was implemented by @tinganho and merged in master recently with 6e69a9e )

The syntax is quite elegant and clean, but can be further improved. It's generally a good rule to try and avoid introducing new keywords. Especially when a very similar one already exists in the language.

I suggest we use the existing instanceof instead. The meaning fits, and it's very familiar to a lay JS folk.

In simple words, to a person with JS background, but a limited TS one (1) leaves more to guess than (2):

function isApple(p1: any): p1 is Apple { }    // (1)   <-- man... let me remember what's the difference...

function isApple(p1: any): p1 instanceof Apple { }    // (2)   <-- hello, I know you!

I do agree that for the C# audience they both are fine. But similarly to bool/boolean swap of the early TS versions, preferring JS over C# is the right thing.

@JsonFreeman
Copy link
Contributor

I'm not sure the is keyword is actually based on C#. That was not my interpretation.

I will say instanceOf has a prototype connotation, whereas is is intended to describe our structural type system. In that light, I actually think instanceOf could be misleading. The user might think that they should only use it when the function implementation does an instanceOf check.

@JsonFreeman JsonFreeman added Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it Suggestion An idea for TypeScript labels Jun 9, 2015
@RyanCavanaugh
Copy link
Member

Experience with re-using an existing value-position keyword as a type-position operator with a different meaning has not proven it to be a good approach.

People are continuously confused about the difference between type-position typeof and value-position typeof -- people try to write typeof T in a type position where T is a type parameter, or think that typeof returns something other than a string.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

3 participants