Repository navigation
allow zero-valent tuple type #13239
Description
Activity
DanielRosenwasser commented
on Jan 1, 2017 MemberMore actionsWhat exactly is the structural distinction between an empty tuple and
undefined[]ornever[]?zpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsthe
lengthproperty that is always0, nopush,shift,unshift,pop,slice,spliceoperations, no index signaturezpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsSergey Rubanov (@chicoxyzzy)
[]to[a]is what0is to1, an identity type if you will:[] + [a] = [a],[a] + [] = [a]https://homotopytypetheory.org/2011/04/18/whats-special-about-identity-types/
still confused?
chicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsAleksey-Bykov TBH I don't understand the use case for such unit type.
chicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsalso
{}is not unit type and it behaves similarly[]zpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsTBH 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
nullandundefinedalso {} 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 aboutconst values : [] = [];chicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsIf
[]will ever exist it should be an alias toany[]similarly to how{}is an alias to{ [propName: string]: any }zpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsany[]implies you can add elements to it:const values: any[] = []; values.push(1);whereas
[]is meant to be empty, that's the differenceconst values: []= []; // hypothetical situation values.push(1); // `push` method is not defined on `[]`chicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsIn that way
[]will behave differently to{}which doesn't hide any object prototype methods so that is counterintuitivezpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionswell 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
ReadOnlyArraythere which hides its prototype methods, riddle me that:https://www.typescriptlang.org/docs/handbook/interfaces.html
chicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsThe 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 ContributorMore actionsAlso as you mentioned you can declare your array unit as
const arrayUnit: ReadonlyArray<any> = [];
zpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsagain, 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 intacthere 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 toReadOnlyArray(except for the index signature andlengthproperty) which has been around since cac3a32Also 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 AuthorMore actionsand 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 sensechicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsconst empty: ReadonlyArray<any> = []; empty.push(0); // error empty = [1, 2, 3]; // error
zpdDG4gta8XKpMCd commented
on Jan 2, 2017 AuthorMore actionsconst 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 latechicoxyzzy commented
on Jan 2, 2017 ContributorMore actionsThis 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 AuthorMore actionseasily
- you made a mistake
const values : ReadOnlyArray<any> = Math.random() > 0.5 ? [1, 2, 3] : []; if (values.length) { // <-- accidently flipped condition const empty : ReadOnlyArray<any> = values; }- renaming
// before const nonEmpty : ReadOnlyArray<any> = [1, 2, 3];someone renamed
nonEmptytoemptyand forgot to fix the declaration// after renaming const empty : ReadOnlyArray<any> = [1, 2, 3]; // no problem, although doesn't make sensezpdDG4gta8XKpMCd commented
on Jan 9, 2017 AuthorMore actionsclosed in favor of #13126
- locked and limited conversation to collaborators
on Jun 19, 2018


the
{}type is allowed, while[]is not allowed, which is a sad news because i was planning to use it as a unit type distinct fromnullandundefinedwhich have their own meaning alreadyconsider allowing the empty tuple type