I am not sure the impact, but wouldn’t that have consequences on the guarantees of runtime representation of records? (eg. you are not sure there are no extraneous fields when receiving a record).
Especially with interop with JS, in this new model there is no guarantee that there are some extra keys in the object that get’s passed to JS, and no way to enforce this at the type level anymore.
No that’s not what it means.
But it’s legit to expect that’s the meaning given how it’s phrased.
You can’t even add back the same field with the same type.
Consider the following, building 3 new types from an existing one in typescript:
// Base type
type CatBase = {
name: string;
age: number;
food: 'fish' | 'milk'
}
/**
* Adds an `id` field to the `Cat` type
* resulting @type {{
* id: number;
* name: string;
* age: number;
* food: 'fish' | 'milk'
* }}
*/
type CatWithId = CatBase & {
id: number;
}
/**
* Removes the `food` field from the `Cat` type
* resulting @type {{ name: string; age: number; }}
*/
type CatWithoutFood = Omit<CatBase, 'food'>;
/**
* Picks only `name` and `food` fields from the `Cat` type
* resulting @type {{ name: string; food: 'fish' | 'milk' }}
*/
type CatNameAndFood = Pick<CatBase, 'name' | 'food'>;
The type spread proposal currently allow to build a CatWithId type from CatBase type, however the type spread allows only to extend a type, for shrinking or reducing a type and build CatWithoutFood type and CatNameAndFoodOnly type from CatBase type, you need the inverse operation of spread which would be type destructure.
hope to provide some answers as interesting as your question, can we come out with something idiomatic? here are some ideas:
type catFood =
| Milk
| Fish
type rec catBase = {
name: string,
age: int,
food: catFood,
friends: array<catBase>,
}
// named type destructure, similar to rescript list destructure
type catWithoutFood = catBase{ name, age, friends }
if all you want to check is that catWithoutFood is a subtype of catBase, you can actually already check it like that, I’m pretty sure it would be erased from the generated output anyway:
type catFood =
| Milk
| Fish
type rec catBase = {
name: string,
age: int,
food: catFood,
friends: array<catBase>,
}
// named type destructure, similar to rescript list destructure
type catWithoutFood = {name: string, age: int, friends: array<catBase>}
let _ = ({name: "foo", age: 0, friends: [], food: Milk} :> catWithoutFood)
the idea is to generate new types from existing ones, so avoiding typing all the fields again when you have an existing type template, extending/shrinking a type can be useful when working with 3rd party types.
The idea of referring to the type of a particular record label seems useful to me.
E.g. in GQL, if you have a record label user.id, and you pass that thing around to leaf components, you don’t necessarily want to duplicate the type definition, but rather piggyback off the GQL type.
In TS, I’d do this:
function UserCard(props: {
userName: Pick<GQL.User, "name">
}) {
}
It’s not exactly what the OP asked for, but it kinda hits in the same curb, I think.
To me these (add/remove/pick) operations would be better suited to being exposed as a library module like Belt.Array / Belt.Option rather than adding customizations to the language/compiler.
Something like Belt.Record. So the code I imagine would look like:
module Record = Belt.Record
type catFood =
| Milk
| Fish
type catBase = {
name: string,
age: int,
food: catFood
}
type catWithId = catBase->Record.add({id: int})
type catWithoutFood = catBase->Record.omit(["food"])
type catNameAndFood = catBase->Record.pick(["name", "food"])
// More imaginary operations
let doesCatHaveFood = catWithoutFood->Record.find("food") // false
let isCatWithoutFoodSubType = catWithoutFood->Record.compareWithField(catBase, "food") // -1 or 0 or 1
This also means we might need to look at justifying having first class types (which goes against adding features to the language), so we could pass types as arguments to functions.
I want ReScript to be small and simple; but with TS features being looked at, we might need to think through the possible options to keep the language lean and simple.
how do you implement those library functions…they will still need language support internally and youre back in the same place but have also added type level functions?