-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Design Meeting Notes, 1/11/2019 #29382
Comments
Interested about Negated types🤩 |
Really dumb edge case for negated types: type FiniteNumber = 0
| 2.225073858507202E-308
| 2.2250738585072024E-308
| 2.225073858507203E-308;
// etc. for every single finite double-precision floating point number, + and -
type NonFiniteNumber = (not FiniteNumber) & number;
function foo (x: NonFiniteNumber | string, y: NonFiniteNumber) {
if (x === y) {
// TS thinks that x is a NonFiniteNumber
// At runtime, x is either +Infinity or -Infinity, but not NaN
}
else {
// TS thinks that x is a string
// At runtime, x could be NaN
}
} This is because The type-checker will probably run out of memory if anyone actually tries to do this (spoiler: don't try to do this), but it's interesting to know that this is theoretically possible. |
It would be great if we would get negated type and they would work when narrowing to truthy string or number primitives (probably with another strict flag). It would be yet another great Typescript feature to find bugs in your code 😍 function fun1(num1? : number){
if(num1){ //ups... should probably be "if(typeof num1 == 'number'){"
// Hypothetical compiler error: "Argument of type 'number & not 0' is not assignable to parameter of type 'number'"" or "Argument of type 'Truthy<number>' is not assignable to parameter of type 'number'""
fun2(num1);
}
}
function fun2(num2: number){
//do sth with num2
} |
A |
ah, of course you're right. I just had this kind of bug recently and got overexcited when I read about negated types 😆 |
Readonly
generalizationsreadonly
.Readonly<T>
today.Partial<T>
) work on tuplesreadonly
modifier for tuple propertiesReadonly
doesn't specially treat arrays.ReadonlyArray
.Array.isArray
work correctly forReadonlyArray
s?Array
is not assignable toReadonlyArray
, but aT
is assignable to aReadonly<T>
.Readonly
to make things readonly-ish consistently, we should generalize this.readonly
expression operatorReadonly<T>
, or just something withreadonly
properties?"readonly
keyword that you contextual type, or usingas readonly
oras const
?<const>[1, 2, 3]
const
might be a better keyword becausereadonly
is only a "view of the world" guarantee; you can't mutate, but someone else might.Negated types
@bterlson: I'm so excited for this
@DanielRosenwasser: I feel a bit more negative about them.
@weswigham: many of my bugs correspond to people trying to express things with strange conditional types.
@rbuckton: Can I make a not-not joke?
Type relationships
not
is related contravariantly on its contained type.not T
≤not U
ifU
≤T
S
≤not T
ifS & T
=never
.not T
≰U
).Interesting topics
number
from a generic typeT
, should we intersect withnot number
? (i.e. should you getT & not number
)?Stringy<T>
=T & string
NonNullable<T>
=T & not null & not undefined
Truthy<T>
=T & not (0 | "" | false | undefined | null)
let x: { a: number } | string
, narrowing withtypeof x === "string"
you'd end up withstring & { a: number }
.Out of time
The text was updated successfully, but these errors were encountered: