GeezerVison is showing github.com — allow zero-valent tuple type · Issue #13239 · microsoft/TypeScript
allow zero-valent tuple type #13239Descriptionthe {} type is allowed, while [] is not allowed, which is a sad news because i was planning to use it as a unit type distinct from null and undefined which have their own meaning already
consider allowing the empty tuple type Reacted by kiara Reacted by Sergey Rubanov ActivityDanielRosenwasser commented on Jan 1, 2017 What exactly is the structural distinction between an empty tuple and undefined[] or never[]? zpdDG4gta8XKpMCd commented on Jan 2, 2017 the length property that is always 0, no push, shift, unshift, pop, slice, splice operations, no index signature zpdDG4gta8XKpMCd commented on Jan 2, 2017 chicoxyzzy commented on Jan 2, 2017 Aleksey-Bykov TBH I don't understand the use case for such unit type. chicoxyzzy commented on Jan 2, 2017 also {} is not unit type and it behaves similarly [] zpdDG4gta8XKpMCd commented on Jan 2, 2017
TBH I don't understand the use case for such unit type.
as i said it could have been yet another unit type in addition to null and undefined
also {} is not unit type and it behaves similarly []
{} is a valid type in TS and [] is gives an error, not sure what you are talking about

chicoxyzzy commented on Jan 2, 2017 If [] will ever exist it should be an alias to any[] similarly to how {} is an alias to { [propName: string]: any } zpdDG4gta8XKpMCd commented on Jan 2, 2017 any[] implies you can add elements to it:
const values: any[] = [];
values.push(1);
whereas [] is meant to be empty, that's the difference
const values: []= []; // hypothetical situation
values.push(1); // `push` method is not defined on `[]`
chicoxyzzy commented on Jan 2, 2017 In that way [] will behave differently to {} which doesn't hide any object prototype methods so that is counterintuitive zpdDG4gta8XKpMCd commented on Jan 2, 2017 well it was never said that [] should behave anything like {} in the first place, not sure why we are trying to compare them
{} which doesn't hide any object prototype methods so that is counterintuitive
does the official TS guideline look counterintuitive to you? yet you can find ReadOnlyArray there which hides its prototype methods, riddle me that:
https://www.typescriptlang.org/docs/handbook/interfaces.html

chicoxyzzy commented on Jan 2, 2017 The counterintuitive part is that [] and {} behaviour will be different in your proposal. Readonly types indicate their behaviour explicitly and that's ok. chicoxyzzy commented on Jan 2, 2017 Also as you mentioned you can declare your array unit as
const arrayUnit: ReadonlyArray<any> = []; zpdDG4gta8XKpMCd commented on Jan 2, 2017 again, i don't care what {} is, it's outside of this topic (as far as i can tell), whatever behaviour it has it should be kept intact
here i was trying to talk about a different type which i called [], and the proposition for this type states that it possesses some unique properties yet very similar to ReadOnlyArray (except for the index signature and length property) which has been around since cac3a32
Also as you mentioned you can declare your array unit
no, i cannot declare it like that because i need it to be nonassignable from ReadOnlyArray<any> zpdDG4gta8XKpMCd commented on Jan 2, 2017 and this is why:
const values: ReadOnlyArray<number> = [1, 2, 3];
let empty: [] = [];
empty = values; //<-- if ReadOnlyArray<a> is assignable, then the whole idea of having a type for an EMPTY tuple doesn't make sense
chicoxyzzy commented on Jan 2, 2017 const empty: ReadonlyArray<any> = [];
empty.push(0); // error
empty = [1, 2, 3]; // error zpdDG4gta8XKpMCd commented on Jan 2, 2017 const empty: ReadonlyArray<any> = [1, 2, 3];
empty.push(0); // error, but who cares it already has [1, 2, 3] instead of being empty
empty = [1, 2, 3]; // error, again too late
chicoxyzzy commented on Jan 2, 2017 This is strange when you assign non-empty array to a constant variable named empty. Can you provide an example? zpdDG4gta8XKpMCd commented on Jan 2, 2017 easily
const values : ReadOnlyArray<any> = Math.random() > 0.5 ? [1, 2, 3] : [];
if (values.length) { // <-- accidently flipped condition
const empty : ReadOnlyArray<any> = values;
}
// before
const nonEmpty : ReadOnlyArray<any> = [1, 2, 3];
someone renamed nonEmpty to empty and forgot to fix the declaration
// after renaming
const empty : ReadOnlyArray<any> = [1, 2, 3]; // no problem, although doesn't make sense
zpdDG4gta8XKpMCd commented on Jan 9, 2017
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|