Skip to content

Use non-nullable types in vscode.d.ts #6907

Closed

Description

TypeScript will introduce support for strict null check and explicit handling of null and undefined - microsoft/TypeScript#7140. This is a great opportunity for us to make the API more explicit and hopefully a chance to prevent programming errors. Parts of the API can move from jsdoc comments into type annotations. For instance, the active editor is declared like this today:

/**
 * The currently active editor or `undefined`. ...
 */
export let activeTextEditor: TextEditor;

and we can move jsdoc type info back into code like so

/**
 * The currently...
 */
export let activeTextEditor: TextEditor | undefined;

Activity

  1. added
    feature-request
    on May 26, 2016
  2. jrieken commented on Oct 10, 2016

    @jrieken
    ContributorAuthor
  3. jrieken commented on Nov 3, 2016

    @jrieken
    ContributorAuthor

    Erich Gamma (@egamma) Dirk Bäumer (@dbaeumer) Looking for some guidance here. Challenge is that many of the providers define method return types ala Foo | Thenable<Foo>. The current behaviour (spec'd in jsdoc) is that they are allowed to return undefined or null sync or async from with in the promise. That results in the following, very verbose, strict-null-aware signature:

    // option 1a, inline
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | undefined | null | Thenable<CodeLens[] | undefined | null>
    }
    
    // option 1b, inline but even more verbose
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | undefined | null | Thenable<CodeLens[]> | Thenable<undefined> | Thenable<null>
    }
    
    // option 1c, one type for null or undefined
    type Nully = null | undefined;
    
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): CodeLens[] | Nully | Thenable<CodeLens[] | Nully>
    }
    
    // option 2, generic result-type
    type Result<T> = T | undefined | null | Thenable<T | undefined | null>
    
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): Result<CodeLens[]>
    }
    
    // option 3, generic nullable-type
    type Nullable<T> = T | undefined | null;    
    
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): Nullable<CodeLens[]> | Thenable<Nullable<CodeLens[]>>
    }

    Options 2 and 3 have the advantage that they can be applied to all providers and make a shorter method signature. On the downside that add another indirection and might make things harder to understand... Tho, also option 1 isn't exactly easy to read and understand. I'd prefer option 1c

  4. dbaeumer commented on Nov 4, 2016

    @dbaeumer
    Member

    Johannes Rieken (@jrieken) hard call: I would actually go for 2.) and name it ProviderResult. Using Nullable makes it bound to nullable. If TS comes up with another feature we might need to change the signature again. With ProviderResult we might be able to simple change that type then.

  5. bpasero commented on Nov 7, 2016

    @bpasero
    Contributor

    +1 for option 2 (generic result-type)

  6. joaomoreno commented on Nov 7, 2016

    @joaomoreno
    Contributor

    I'd combine 2 and 3, each a different problem in itself, but composable with each other:

    type Maybe<T> = T | undefined | null;
    type Result<T> = Maybe<T> | Thenable<Maybe<T>>;
    
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): Result<T>;
    }
  7. aeschli commented on Nov 7, 2016

    @aeschli
    Contributor

    Wanted to suggest Optional<T> but actually like Maybe<T> even better.
    +1 for option 3 with Maybe<T>.
    I'd rather not combine it with 2, too many types, and, especially, 'Result' is too general.

  8. joaomoreno commented on Nov 7, 2016

    @joaomoreno
    Contributor
    type Eventually<T> = Maybe<T> | Thenable<Maybe<T>>;

    😄

  9. chrmarti commented on Nov 7, 2016

    @chrmarti
    Collaborator

    Keeping the two concepts separate might be easier to read and would allow for more variants of reuse:

    type Maybe<T> = T | undefined | null;
    type Eventually<T> = T | Thenable<T>;
    
    interface CodeLensProvider {
      provideCodeLenses(document: TextDocument, token: CancellationToken): Eventually<Maybe<CodeLens[]>>;
    }
  10. 9 remaining items

  11. jrieken commented on Nov 23, 2016

    @jrieken
    ContributorAuthor

    Andre Weinand (@weinand) - to program against the latest API do the following:

    • open your extensions package.json file
    • change the engine.vscode property to *
    • run npm run postinstall (assuming you have the vscode module as dev-dependency)
    • undo the engine-version change from step 2

    note tho that this no also depends on TypeScript 2.0

  12. weinand commented on Nov 23, 2016

    @weinand
    Contributor

    Johannes Rieken (@jrieken) done. That was easy.
    But I have to set "strictNullChecks": truein my tsconfig.json so that the compiler actually checks for this.

  13. ramya-rao-a commented on Dec 5, 2016

    @ramya-rao-a
    Contributor

    Johannes Rieken (@jrieken) The test item #16515 is linked here, but the test item does not have anything on non-nullable types.

  14. added
    feature-request
    verification-needed
    and removed
    feature-request
    on Dec 6, 2016
  15. jrieken commented on Dec 6, 2016

    @jrieken
    ContributorAuthor

    Ramya Rao (@ramya-rao-a) This was meant for verification-needed but I have special verifier in mind: those that have already updated their extensions to use strict null checks. I have Matt Bierner (@mjbvz) (ts/js) and João Moreno (@joaomoreno) (git exploration) in mind

  16. added
    verified
    on Dec 7, 2016
  17. locked and limited conversation to collaborators on Nov 18, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

apidebtfeature-requestverification-neededverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions