6 ms·
To provide a concrete example, this bit me in a typescript codebase: type Option<T> = T | undefined function f<T>(value: T): Option<T> { ... } le
by ekimekim 8mo ago
To provide a concrete example, this bit me in a typescript codebase:
type Option<T> = T | undefined
function f<T>(value: T): Option<T> { ... }
let thing: string | undefined = undefined;
let result = f(thing);
Now imagine the definition of Option is in some library or other file and you don't realize how it works. You are thinking of the Option as its own structure and expect f to return Option<string | undefined>. But Option<string | undefined> = string | undefined | undefined = string | undefined = Option<string>.
The mistake here is in how Option is defined, but it's a footgun you need to be aware of.
- int_19h 8mo agoThe mistake here is that you assume that Option is a struct. For TypeScript, this is an idiomatic definition of something that is optional though.