gcanti / newtype-ts

Implementation of newtypes in TypeScript
https://gcanti.github.io/newtype-ts/
MIT License
582 stars 14 forks source link

prismNonNegative.getOption('0.5' as unknown as number) would return Some('0.5') #34

Closed dearlordylord closed 1 year ago

dearlordylord commented 1 year ago

🐛 Bug report

tried to a prism to define some parsing/validation behavior, and found out that

prismNonNegative.getOption('0.5' as unknown as number) would return Some('0.5') whereas '0.5' is a string

Current Behavior

Some('0.5')

Expected behavior

None

Reproducible example

prismNonNegative.getOption('0.5' as unknown as number)

Suggested solution(s)

return None

Additional context

I also noticed that prismNonNegative.getOption('a' as unknown as number) would return None so it looks more of a bug than of a feature and me breaking typing with 'as unknown'

Your environment

Software Version(s)
newtype-ts 0.3.5
TypeScript 5.1
gcanti commented 1 year ago

@Firfi this is "undefined behaviour":

prismNonNegative.getOption('0.5' as unknown as number)

since you are breaking the precondition (input must be a number)

I also noticed that prismNonNegative.getOption('a' as unknown as number) would return None so it looks more of a bug than of a feature and me breaking typing with 'as unknown'

this is because

console.log('0.5' >= 0) // true
console.log('a' >= 0) // false

See https://github.com/gcanti/newtype-ts/blob/04b8f4ed9b47dcff2c485693678105821f156e52/src/NonNegative.ts#L15