Skip to content

allow zero-valent tuple type #13239

Description

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 from null and undefined which have their own meaning already

consider allowing the empty tuple type

Activity

  1. DanielRosenwasser commented on Jan 1, 2017

    @DanielRosenwasser
    Member

    What exactly is the structural distinction between an empty tuple and undefined[] or never[]?

  2. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    the length property that is always 0, no push, shift, unshift, pop, slice, splice operations, no index signature

  3. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    Sergey Rubanov (@chicoxyzzy) [] to [a] is what 0 is to 1, an identity type if you will: [] + [a] = [a], [a] + [] = [a]

    https://homotopytypetheory.org/2011/04/18/whats-special-about-identity-types/

    still confused?

  4. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    Aleksey-Bykov TBH I don't understand the use case for such unit type.

  5. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    also {} is not unit type and it behaves similarly []

  6. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    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

    const values : [] = [];
    

    image

  7. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    If [] will ever exist it should be an alias to any[] similarly to how {} is an alias to { [propName: string]: any }

  8. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    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 `[]`
    
  9. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    In that way [] will behave differently to {} which doesn't hide any object prototype methods so that is counterintuitive

  10. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    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

    image

  11. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    The counterintuitive part is that [] and {} behaviour will be different in your proposal. Readonly types indicate their behaviour explicitly and that's ok.

  12. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    Also as you mentioned you can declare your array unit as

    const arrayUnit: ReadonlyArray<any> = [];
  13. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    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>

  14. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    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
    
  15. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor
    const empty: ReadonlyArray<any> = [];
    
    empty.push(0);     // error
    empty = [1, 2, 3]; // error
  16. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author
    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
    
  17. chicoxyzzy commented on Jan 2, 2017

    @chicoxyzzy
    Contributor

    This is strange when you assign non-empty array to a constant variable named empty. Can you provide an example?

  18. zpdDG4gta8XKpMCd commented on Jan 2, 2017

    @zpdDG4gta8XKpMCd
    Author

    easily

    • 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 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
    
  19. zpdDG4gta8XKpMCd commented on Jan 9, 2017

    @zpdDG4gta8XKpMCd
    Author

    closed in favor of #13126

  20. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions