Wheelwright
Wheelwright
TTCTheo's Typesafe Cult
Created by Wheelwright on 3/26/2024 in #questions
discriminate unions based on return type
type Good = { is: true; _tag: "good" };
type Bad = { is: false; _tag: "bad" };

// I know ts can discriminate a union type
function x(y: Good | Bad) {
if (y.is) return;
// then we know _tag must be 'bad'
y._tag;
}

type Fortunate = { getIs: () => true; _tag: "good" };
type Unlucky = { getIs: () => false; _tag: "bad" };

// but I want to discriminate based off the return type
function z(yy: Fortunate | Unlucky) {
// this what I want to achieve
if (yy.getIs()) return;
// but _tag is still unknown
yy._tag;
}
type Good = { is: true; _tag: "good" };
type Bad = { is: false; _tag: "bad" };

// I know ts can discriminate a union type
function x(y: Good | Bad) {
if (y.is) return;
// then we know _tag must be 'bad'
y._tag;
}

type Fortunate = { getIs: () => true; _tag: "good" };
type Unlucky = { getIs: () => false; _tag: "bad" };

// but I want to discriminate based off the return type
function z(yy: Fortunate | Unlucky) {
// this what I want to achieve
if (yy.getIs()) return;
// but _tag is still unknown
yy._tag;
}
It doesn't seem to be a supported feature but I don't know all the advanced features of ts so I wondered if there's a way of doing this with a more advanced utility type. tyvm
2 replies
TTCTheo's Typesafe Cult
Created by Wheelwright on 2/28/2024 in #questions
pushing back against unit testing
I encountered someone recently who is pushing for test coverage and unit testing. I’m a full stack dev writing ts when I can and js when I have to, they’re a Java dev. They cited some data saying unit tests reduce bugs by a lot. However my company doesn’t even use typescript on a lot of our products. I’m looking for some info comparing the efficacy of good ts implementation vs good unit testing of js. Tyvm
1 replies