Gathering detailed insights and metrics for @gregchlosta/option-ts
Gathering detailed insights and metrics for @gregchlosta/option-ts
npm install @gregchlosta/option-ts
Typescript
Module System
Node Version
NPM Version
69.8
Supply Chain
93.4
Quality
75.6
Maintenance
100
Vulnerability
100
License
TypeScript (83.58%)
JavaScript (16.42%)
Total Downloads
1,343
Last Day
10
Last Week
14
Last Month
29
Last Year
201
2 Stars
15 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.1.2
Package Id
@gregchlosta/option-ts@0.1.2
Unpacked Size
9.46 kB
Size
3.65 kB
File Count
15
NPM Version
8.19.2
Node Version
18.12.1
Cumulative downloads
Total Downloads
Last day
0%
10
Compared to previous day
Last week
0%
14
Compared to previous week
Last month
141.7%
29
Compared to previous month
Last year
-50.7%
201
Compared to previous year
Simple implementation of Option type with Pattern Matching for TypesScript
Option<T>
and Its Advantages Over Null ValuesThe problem with null values is that if you try to use a null value as a not-null value, you’ll get an error of some kind. Because this null or not-null property is pervasive, it’s extremely easy to make this kind of error.
However, the concept that null is trying to express is still a useful one: a null is a value that is currently invalid or absent for some reason.
The problem isn’t really with the concept but with the particular implementation. Type Option<T>
with Pattern Matching functionality provided by this library can help you with this.
1npm install @gregchlosta/option-ts 2
Option<T>
is a discriminated union of Some<T>
and None
. When we have a Some value, we know that a value is present and the value is held within the Some. When we have a None value, in some sense, it means the same thing as null. We don’t have a valid value.
Option<T>
value1import { Option, newOption } from '@gregchlosta/option-ts' 2 3// Type will automatically get inferred when value is provided as parameter. 4const some = newOption('Some Value') // -> Option<string> -> (Some<string>) 5 6// When creating a None Option, manual typing possible value type is required since automatic type inferrance is not possible 7const none: Option<string> = newOption() // -> Option<string> -> (None)
match()
Match function will take Option<T>
, and matcher object as parameters. Functions declared in the matcher object always have to return the same value type.
Example below come from React, but option-ts can be used with any other front-end framework or regular TypeScript.
1import React from 'react' 2import { Option, match } from '@gregchlosta/option-ts' 3 4export type ButtonProps = { 5 title: Option<string> 6} 7 8export const Button: React.FC<ButtonProps> = ({ title }) => { 9 return match(title, { 10 Some: (v) => <button>{v}</button>, 11 None: () => <button>Default</button>, 12 }) 13}
Option<T>
with withDefault()
To get direct access to the value we can simple unpack option using withDefault()
which takes two parameters: option, and default value.
1import { Option, newOption, withDefault } from '@gregchlosta/option-ts' 2 3const someOption = newOption('Some!') 4const noneOption: Option<string> = newOption() 5 6const valueSome = withDefault(someOption, 'default') // 'Some!' 7const valueDefault = withDefault(noneOption, 'default') // 'default'
1import { newOption, isSome, isNone } from '@gregchlosta/option-ts' 2 3const someOption = newOption('Some!') 4const noneOption: Option<string> = newOption() 5 6isSome(someOption) // true 7isNone(noneOption) // false
The MIT License
No vulnerabilities found.
No security vulnerabilities found.