4 ms·
Yes I see what you mean, it'd be interesting to see what the exact rules around type checking are; especially when dealing with []. The string type for example,
by wingspan 14y ago
Yes I see what you mean, it'd be interesting to see what the exact rules around type checking are; especially when dealing with []. The string type for example, always give you a string when indexed, even when incorrect:
var s = 'foo';
var l = s['length'];
// type of l is `string`
I also agree about non-nullable types, and I've found even with TypeScript, having to make sure your vars are defined and not-null is still a pain.
- masklinn 14y ago> var s = 'foo'; > var l = s['length']; > // type of l is `string` beg pardon? $ js js> var s = 'foo'; js> s['length']; 3 js> typeof s['length']; number $ node > var s = 'foo'; undefined > s['length']; 3 > typeof s['length']; 'number'
- wingspan 14y agoPlease read the whole post for context. I am talking about the type system in TypeScript thinking that `l` is a string (which you can find out, for instance, by hovering over the `var` keyword in Visual Studio), when in fact, as you pointed out, it is a number. I assume this is because TypeScript caters to the most common case of indexing a string to obtain a single character (another string, basically).
- deleted 14y ago[deleted]