Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh

OliverJAsh/test.ts

Created Aug 2, 2019
Embed
What would you like to do?
TypeScript narrow non-discriminated union
{
type AncestryType = {
type: string;
};
type AncestryCategory = {
type: string;
category: string;
};
type AncestrySubcategory = {
type: string;
category: string;
subcategory: string;
};
type Ancestry = AncestryType | AncestryCategory | AncestrySubcategory;
const fn1 = (ancestry: Ancestry) => {
if (!('category' in ancestry)) {
ancestry; // ✅ AncestryType
} else {
// ✅ AncestryCategory | AncestrySubcategory
ancestry;
}
};
const checkAncestryIsType = (ancestry: Ancestry): ancestry is AncestryType =>
!('category' in ancestry);
const fn2 = (ancestry: Ancestry) => {
if (checkAncestryIsType(ancestry)) {
ancestry; // ✅ AncestryType
} else {
/*
Actual: never
Expected: AncestryCategory | AncestrySubcategory
*/
ancestry;
}
};
}
//
// Workaround
// https://github.com/microsoft/TypeScript/issues/32680#issuecomment-517726428
//
{
type AncestryType = {
_type: 'type';
type: string;
};
type AncestryCategory = {
_type: 'category';
type: string;
category: string;
};
type AncestrySubcategory = {
_type: 'subcategory';
type: string;
category: string;
subcategory: string;
};
type Ancestry = AncestryType | AncestryCategory | AncestrySubcategory;
const checkAncestryIsType = (ancestry: Ancestry): ancestry is AncestryType =>
!('category' in ancestry);
const fn1 = (ancestry: Ancestry) => {
if (checkAncestryIsType(ancestry)) {
ancestry; // ✅ AncestryType
} else {
ancestry; // ✅ AncestryCategory | AncestrySubcategory
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.