-
Notifications
You must be signed in to change notification settings - Fork 12.8k
User-defined type guards and structural typing - possible failure #5518
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
Comments
A user-defined type guard function, just like the instance of type will filter out types that are assignable to target type specified by the guard. in your example, In the typescript compiler code base we use intersection types to create tags on types we want to treat as nominal. see https://github.com/Microsoft/TypeScript/blob/master/src/compiler/types.ts#L8. |
As a minor correction, we use the subtype relation, not assignability. |
Thanks @mhegazy for the pointer (and @DanielRosenwasser for the extra bit of understanding) - great to have conversations with the TypeScript devs so easily and quickly! It's certainly not a bad approach (I had read #4895 a week or so ago which gives the idea some thorough treatment!). The I'll give it a try and report back. |
OK. Done :) This code works well in the playground. I originally was trying the branding this way
That gave a LOT of mess and I had a huge post written about using functions, inlining, casting, etc. I then gave it some thought and realised I instead should have had
Full playground code (for others)...
Thanks very much!! :) I had to change just two spots in my code where I returned something that was typed as a string (neither was a bug, as the string was a guid, but it's nice this will now be flagged for me). |
Note that the code wouldn't have been as easily changed if I had
since that's effectively my first interface above and things like
|
Hi,
Write this code in the playground
You'll see an error in the call
iTakeStrings(guidOrString);
because the type of guidOrString here is deemed to be{}
rather thanstring
. I'm guessing this is because of structural typing. This is evident (in my mind) because if you uncomment the// dummyExtra: number;
line then there's no error.I've got types that are notionally guids in my code (lots of interfaces generated from my C# DTOs using some T4 templates) but really they're strings. I have toLowerCase() specified in my Guid interface as a way of differentiating the guids from the
any
type and also because I sometimes need to call toLowerCase() on them anyway.Is it possible to support this? I'm guessing the types look similar but
string
has a lot more than what myGuid
type has (eg length, toUpperCase, etc) so the types aren't the same really.I thought this was a regression introduced by TypeScript v1.7.2 that I've got with VS 2015 Update 1 RC, but I then checked on VS 2015 RTM with TypeScript v1.6, and in the playground (showing the version on there would be handy!) and the problem exists in all of them.
Thanks for reading. I've tried searching other issues but couldn't see one quite matching this scenario, particularly as the code works when you restore the commented-out line.
The text was updated successfully, but these errors were encountered: