saguiar | 9 years ago | on: Gödel's Incompleteness Theorems (2015)
saguiar's comments
saguiar | 9 years ago | on: If TypeScript is so great, how come all notable ReactJS projects use Babel?
> What I really miss is the ability to say that what is the return type of the method
Agreed! There's an open issue that addresses that scenario (and a ton of others):
saguiar | 9 years ago | on: If TypeScript is so great, how come all notable ReactJS projects use Babel?
I've been using discriminated unions on TS 2 to do this, and works quite well.
Example:
const INCREMENT: "increment" = "increment";
const DECREMENT: "decrement" = "decrement";
interface Increment {
type: typeof INCREMENT;
payload: {
inc: number;
};
}
interface Decrement {
type: typeof DECREMENT;
payload: {
dec: number;
};
}
interface State {
count: number;
}
type Actions = Increment | Decrement;
function reducer(state: State, action: Actions) {
switch (action.type) {
case INCREMENT: {
const { inc } = action.payload;
return { count: state.count + inc };
}
case DECREMENT: {
const { dec } = action.payload;
return { count: state.count + dec };
}
default:
return state;
}
}
Once TS figures out the action type is INCREMENT, it can infer the type of the payload. You don't need the Actions type either, you can just receive action: Increment | Decrement.saguiar | 9 years ago | on: Plottable.js – Flexible, interactive charts for the web
Thanks, it does make more sense now.
saguiar | 9 years ago | on: Plottable.js – Flexible, interactive charts for the web
The odd thing is that they are not using three:
public connectorsEnabled(enabled: boolean): this;
public connectorsEnabled(): boolean;
public connectorsEnabled(enabled?: boolean): any {
if (enabled == null) {
return this._connectorsEnabled;
}
this._connectorsEnabled = enabled;
return this;
}
In that way, typescript will use the most specific overload to do type checking, so you'll get an actual boolean when doing connectorsEnabled(); instead of any.
page 1
(though maybe in a completely irrelevant sense of truth).