3 ms·
I think I like the duck typing in TypeScript more than I dislike it. But I still wish I could say “this function accepts a type called RobotName. It’s a string
by Waterluvian 4y ago
I think I like the duck typing in TypeScript more than I dislike it.
But I still wish I could say “this function accepts a type called RobotName. It’s a string, but so is RobotUuid, and we don’t want that. So only accept, strictly, objects typed as RobotName.”
- malf 4y agotype robotname = string & {_robotname_marker:true} or some variation. The marker does not actually need to exist.
- jzig 4y agotype RobotName = string; function foo(r: RobotName) {} ?
- chpatrick 4y agoIn TypeScript any type that's structurally the same is considered equal, type just gives it a different name.
- Waterluvian 4y agoYep. But that function will accept type RobotUuid = string; const bar: RobotUuid = “abc…”; foo(bar); This is what duck typing is and specifically my curiosity about being able to de-duck on demand.
- roflyear 4y agoYeah just means you have to rely on linting and your ide to catch those errors. And hopefully the rest of your team does the same thing. Tho I suppose if it is really important you can put an assert there but I'm not familiar with that wrt typescript, maybe the transpiler would kill that? I've done the occasional type checking in that way in similar languages, it is kind of self documenting too. 90% of the time duck typing is what you want.
- tazard 4y agoIf robot0 name is AAA and uid Is BBB, and robot 1 name is BBB and uid AAA, and you call the function checkRobot('AAA'), what sort of assert exactly could differentiate between a name and a uid?
- roflyear 4y agoI don't think it is possible in Typescript, but in other languages you can do assertations about custom types. Like assert istype(whatever, MyCustomType) Which would throw an exception if whatever is not a "MyCustomType" at runtime.
- tshaddox 4y ago> Yeah just means you have to rely on linting and your ide to catch those errors. And hopefully the rest of your team does the same thing. Yes, and that’s roughly what TypeScript is: a linter that everyone on your team is running.
- chpatrick 4y agoYou can kind of do it: interface RobotName { value: string; type: "RobotName"; } Or if you don't want to make an extra wrapper around every object: interface RobotName { _phantomType: "RobotName"; } function makeRobotName(name: string): RobotName { return name as any as RobotName; } function getRobotName(robotName: RobotName): string { return robotName as any as string; } Presumably V8 is smart enough to inline the wrapper functions.
- Waterluvian 4y agoOh cool. So you basically lie about the runtime structure of the type, which works out fine during type checking at compilation.
- Waterluvian 4y agoDiscriminators are super power powerful. Love them. But they can be just so heavy for things.
- tshaddox 4y agoThe closest way to get nominal typing in TypeScript is with type branding. I haven’t actually used this small library, but it illustrates the idea: https://github.com/kourge/ts-brand https://github.com/kourge/ts-brand
- Waterluvian 4y ago“nominal typing” thanks for the link and terminology!
- mejutoco 4y agoIn haskell this is done with newtype. Another example would be using km and miles, and making sure they are not mixed. Here there is an interesting article about doing this in typescript (not affiliated) https://kubyshkin.name/posts/newtype-in-typescript/ https://kubyshkin.name/posts/newtype-in-typescript/
- jks 4y agoWay back in the 1990s I worked in a place where we had to use Ada and follow a strict style guide. The guide prohibited using raw numeric types (such as "unsigned int" or "double" in C-like languages) and required creating a new type (https://en.wikibooks.org/wiki/Ada_Programming/Type_System#Defining_new_types_and_subtypes https://en.wikibooks.org/wiki/Ada_Programming/Type_System#De...) for each different quantity, such as temperature or speed. Then you couldn't assign a speed value to a temperature variable, or compare them to each other or do arithmetic, unless you specifically define what the operators mean. It felt like a lot of busywork, but it did prevent some classes of bugs.
- ThePadawan 4y agoIn the mid-2010s I worked at a company that switched from using raw ints to refer to database rows to using (in C# terms) "Id<FooTable>". Across the entire codebase, we discovered an entire class of bugs that only never cause any issues because all the important rows in all the important tables had Id 1 (e.g. Currency 1 was USD and Country 1 was USA) - so in a few places where the ints got mixed up, the correct row was still accidentally looked up in the wrong table).
- russianGuy83829 4y agohow did that work out in the end?
- ThePadawan 4y agoI mean, no problem ever occurred. Things just accidentally worked even though they shouldn't, and that isn't a bug. The introduction of the additional types everywhere did turn into massive headaches based around dependency management and versioning. So overall, I was personally disappointed in the results, but nevertheless happy to work with less "icky" feeling code.
- Waterluvian 4y agoOh wow. Was there a “stomach sank to the floor” sudden feeling upon discovery?
- akama 4y agoThis is actually one of the nice features about OCaml that comes in handy when you have a function that takes two arguments of the same type but don't necessarily need to make a whole new type for each of them. They are called Labelled Arguments [0] and when you call the function, the label has to be the same. I've found that using it can clean up code because it ensures that variables share the name across the codebase as well as making sure arguments don't get mixed up. [0]: https://ocaml.org/docs/labels https://ocaml.org/docs/labels
- Jasper_ 4y agoThe simplest way I know of to make a discriminated number type in TypeScript is: declare const isRobotID: unique symbol; type RobotID = number & { [isRobotID]: true }; and now you can cast a number to a RobotID and back.