generated from CodelyTV/typescript-basic-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(narrowing): add example to discriminate union with prop value
- Loading branch information
1 parent
e80c705
commit fdbf35a
Showing
3 changed files
with
12 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[]) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/narrowing/discriminate-union-type/calculateStepPoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |