Skip to content

selectionStart should not be nullable: TSJS-lib-generator regression in 2.8.x #23094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
styfle opened this issue Apr 2, 2018 · 5 comments
Closed
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@styfle
Copy link
Contributor

styfle commented Apr 2, 2018

TypeScript Version: 2.8.1

Search Terms:
selectionStart, selectionEnd

Code

// Source code from https://github.com/styfle/copee
function fromElement(el: HTMLInputElement | HTMLTextAreaElement, preserveSelection?: boolean) {
    let start = 0;
    let end = 0;
    let success = false;

    if (preserveSelection) {
        start = el.selectionStart;
        end = el.selectionEnd;
    }

    el.select();

    try {
        success = document.execCommand('copy');
    } catch (err) {
        success = false;
    }

    if (preserveSelection) {
        el.setSelectionRange(start, end);
    }

    return success;
}

Expected behavior:
el.selectionStart and el.selectionEnd should return number

Actual behavior:
el.selectionStart and el.selectionEnd actually return number | null

Playground Link:
Playground

Related Issues:
#22381 , styfle/copee#21

@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

looking at the spec, seems like null is a possible value for selectionStart and selectionEnd.

@mhegazy mhegazy added the Needs More Info The issue still hasn't been fully clarified label Apr 2, 2018
@styfle
Copy link
Contributor Author

styfle commented Apr 2, 2018

@mhegazy Interesting. It looks like type=text is never null but type=email is null for example.

  1. Is there a way to tell typescript that the HTMLInputElement is type=text?
  2. Also, why did this change in TS 2.8? Was it just wrong the whole time?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

Is there a way to tell typescript that the HTMLInputElement is type=text?

Do not think there is a simple way to do this really.

Also, why did this change in TS 2.8?

We auto-generate the library from the webidl file from edge. see https://github.com/Microsoft/TSJS-lib-generator. We have taken an update to the file in 2.8.

Was it just wrong the whole time?

the input file can have bugs, and it can be wrong.. so yes.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 2, 2018

if you are sure it is not null, you can use the null assertion operator !

@styfle
Copy link
Contributor Author

styfle commented Apr 2, 2018

Oh yeah, the null assertion operator is new.

But now that I think about it, the user may pass in an input element that I don't the type.

So I'll just fallback to 0, just in case.

Thanks 👍

@styfle styfle closed this as completed Apr 2, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants