Description
TypeScript Version: 2.8.1 (MS Build NuGet package)
Using "latest" Visual Studio 2017 plugin - v15.6.20202.3
Was working fine all the way through to 2.7.x until trying to update to released 2.8.x this morning
Search Terms: keyof union
Code
//function to conveniently allow passing in a guid, or an object that contains a guid at a known property
declare function extractGuid<T>(guidOrObject: Guid | T, idProperty: keyof T): Guid;
//GUIDs are really just strings, so treat them as such. Adding toLowerCase() is a good way of enforcing this
interface Guid {
toLowerCase(): string;
__GuidBrand?: any;
}
interface Person {
id: Guid,
name: string,
address: string
}
var c1: Guid = '12341234-1234-1234-1234-123412341234';
var g1 = extractGuid(c1, 'id'); //'id' was previously ignored
var c2: Person = {
id: '12341234-1234-1234-1234-123412341234',
name: 'my name',
address: '1 way street'
};
var g2 = extractGuid(c2, 'id');
Expected behavior:
I don't get a compilation error on the line var g1 = ....
The 'id' property is effectively ignored because the T
isn't being used
Actual behavior:
I get an error (also shown in playground below)
Argument of type '"id"' is not assignable to parameter of type '"toLowerCase" | "__GuidBrand?"'
Related Issues:
Maybe this one
#22300 (comment)
It talks about keyof and intersection (not union) types and branding similar to what I'm doing with the branded Guid. Not entirely sure it's related but it at least fits the timeline of being in 2.8 and not in 2.7.