Skip to content

Commit

Permalink
feat(narrowing): add example to discriminate union with prop value
Browse files Browse the repository at this point in the history
  • Loading branch information
ismanapa authored and JavierCane committed Jun 14, 2022
1 parent e80c705 commit fdbf35a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/narrowing/discriminate-union-type/QuizStep.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export class QuizStep {
readonly type = "quiz";
constructor(readonly name: string, readonly questions: string[]) {}
}
1 change: 1 addition & 0 deletions src/narrowing/discriminate-union-type/VideoStep.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export class VideoStep {
readonly type = "video";
constructor(readonly name: string, readonly durationInMillis: number) {}
}
10 changes: 10 additions & 0 deletions src/narrowing/discriminate-union-type/calculateStepPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { QuizStep } from "./QuizStep";
import { VideoStep } from "./VideoStep";

export function calculateStepPoints(step: VideoStep | QuizStep) {
if (step.type === "video") {
return step.durationInMillis / 1000;
}

return step.questions.length * 50;
}

0 comments on commit fdbf35a

Please sign in to comment.