Typescript: declare that ALL properties on an object must be of the same type

Solution 1: Indexable type interface Thing { name: string } interface ThingMap { [thingName: string]: Thing } const allTheThings: ThingMap = { first: { name: “first thing name” }, second: { name: “second thing name” }, third: { name: “third thing name” }, } The downside here is that you’d be able to access any … Read more

When to use const void*?

const int *var; const is a contract. By receiving a const int * parameter, you “tell” the caller that you (the called function) will not modify the objects the pointer points to. Your second example explicitly breaks that contract by casting away the const qualifier and then modifying the object pointed by the received pointer. … Read more

Type-safety in C

The problem is that C doesn’t treat your two typedefs as distinctive types, because they are both type unsigned. There are various tricks to dodge this. One thing would be to change your types to enums. Good compilers will enforce stronger typing warnings on implicit conversions to/from a certain enum type to any other type. … Read more

Safe modelling of relational data in Haskell

The ixset library (or ixset-typed, a more type-safe version) will help you with this. It’s the library that backs the relational part of acid-state, which also handles versioned serialization of your data and/or concurrency guarantees, in case you need it. The Happstack Book has an IxSet tutorial. The thing about ixset is that it manages … Read more

Is there a way to represent a non-negative integer in TypeScript so that the compiler would prevent using fractions and negatives?

Update 2021: Yes, template literals allow this to be done; observe: type NonNegativeInteger<T extends number> = number extends T ? never : `${T}` extends `-${string}` | `${string}.${string}` ? never : T; Note that number extends T is necessary to restrict a general number type. Usage: function negate<N extends number>(n: NonNegativeInteger<N>): number { return -n; } … Read more

Int vs Word in common use?

It is true that the expressiveness of the Haskell type system encourages users to assign precise types to entities they define. However, seasoned Haskellers will readily acknowledge that one must strike a balance between ultimate type precision (which besides isn’t always attainable given the current limits of Haskell type system) and convenience. In short, precise … Read more

Which Typesafe Enum in C++ Are You Using?

I’m currently playing around with the Boost.Enum proposal from the Boost Vault (filename enum_rev4.6.zip). Although it was never officially submitted for inclusion into Boost, it’s useable as-is. (Documentation is lacking but is made up for by clear source code and good tests.) Boost.Enum lets you declare an enum like this: BOOST_ENUM_VALUES(Level, const char*, (Abort)(“unrecoverable problem”) … Read more