The Wayback Machine - https://web.archive.org/web/20221205154322/https://github.com/microsoft/TypeScript/pull/38049
Skip to content
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

Report primitive type in literal-to-primitive relation complaints #38049

Conversation

JoshuaKGoldberg
Copy link
Contributor

@JoshuaKGoldberg JoshuaKGoldberg commented Apr 19, 2020

Fixes #29861.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 20, 2020

@typescript-bot pack this

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 20, 2020

Heya @DanielRosenwasser, I've started to run the tarball bundle task on this PR at 035b9ac. You can monitor the build here.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 20, 2020

Do we have a regression for something like this?

let x: "hello" | "world";
let y: "world" | number;
y = x;

My fear is that this gives an error for string is not assignable to string | number but GitHub makes it so hard to review the changes.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 20, 2020

Hey @DanielRosenwasser, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/71654/artifacts?artifactName=tgz&fileId=4F84A2EE356018F5BE2BA2B18081291312BF84EC551EC982861945C22B635B1802&fileName=/typescript-3.9.0-insiders.20200420.tgz"
    }
}

and then running npm install.


There is also a playground for this build.

@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Apr 20, 2020

Ah, correct, that is a regression. I can take that on soon!

@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented Apr 21, 2020

d12b741 excludes type unions from either the source or target types in reporting. Looks like that extends through reporting chains, where either can be unions.

@@ -1,6 +1,6 @@
tests/cases/compiler/a.ts(2,6): error TS1023: An index signature parameter type must be either 'string' or 'number'.
tests/cases/compiler/a.ts(8,11): error TS2538: Type '1n' cannot be used as an index type.
tests/cases/compiler/a.ts(14,1): error TS2322: Type 'bigint' is not assignable to type 'string | number | symbol'.
tests/cases/compiler/a.ts(14,1): error TS2322: Type '123n' is not assignable to type 'string | number | symbol'.
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this happen?

Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, the target is a union.

src/compiler/checker.ts Outdated Show resolved Hide resolved
Type '"text"' is not assignable to type 'T'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'string' is not assignable to type 'ChannelOfType<T, TextChannel>["type"]'.
Type 'string' is not assignable to type 'T & "text"'.
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...

'0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
Type '"" | 0' is not assignable to type 'T'.
Type 'string | number' is not assignable to type 'T'.
'"" | 0' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm...

!!! error TS2345: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
var r11b = foo3(1, (x: T) => '', 1); // error
~~~~~~~~~~~~
!!! error TS2345: Argument of type '(x: T) => string' is not assignable to parameter of type '(a: 1) => string'.
!!! error TS2345: Types of parameters 'x' and 'a' are incompatible.
!!! error TS2345: Type '1' is not assignable to type 'T'.
!!! error TS2345: Type 'number' is not assignable to type 'T'.
!!! error TS2345: 'T' could be instantiated with an arbitrary type which could be unrelated to '1'.
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm...

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Okay, one thing I've noticed is that this doesn't work with weird intersections of singleton types and with generics constrained to the type of interest. Maybe instead of forEachType(target, isUnitType), try using the following helper function.

function typeCouldHaveNoTopLevelSingletonTypes(type: Type) {
    return forEachType(type, worker);
}

function typeCouldHaveNoTopLevelSingletonTypesWorker(type: Type) {
    return (type.flags & TypeFlags.Intersection)
        ? forEach((type as IntersectionType).types, typeCouldHaveNoTopLevelSingletonTypesWorker)
        : isUnitType(type) || !!(type.flags & TypeFlags.Instantiable);
}

return forEachType(type, typeCouldHaveNoTopLevelSingletonTypesWorker);
}

function typeCouldHaveNoTopLevelSingletonTypesWorker(type: Type): boolean {
Copy link
Member

@DanielRosenwasser DanielRosenwasser Apr 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I think we're getting there. First, I would move these functions out so that they don't add more to the current closure on every call.

Second, I think we have to go deeper on constraints, and we can get rid of the "Worker" function.

            function typeCouldHaveNoTopLevelSingletonTypes(type: Type): boolean {
                if (type.flags & TypeFlags.UnionOrIntersection) {
                    return !!forEach((type as IntersectionType).types, typeCouldHaveNoTopLevelSingletonTypes);
                }

                if (type.flags & TypeFlags.Instantiable) {
                    const constraint = getConstraintOfType(type);
                    if (constraint) {
                        return typeCouldHaveNoTopLevelSingletonTypes(constraint);
                    }
                }

                return isUnitType(type);
            }

@sandersn sandersn added this to Not started in PR Backlog Apr 30, 2020
@sandersn sandersn added the For Backlog Bug PRs that fix a backlog bug label May 5, 2020
@sandersn sandersn moved this from Not started to Waiting on author in PR Backlog May 5, 2020
@sandersn
Copy link
Member

sandersn commented May 21, 2020

@JoshuaKGoldberg Do you want to keep working on this? It looks like this just needs a small refactoring suggested by @DanielRosenwasser (although I didn't understand his comment "I think we have to go deeper on constraints".)

@JoshuaKGoldberg
Copy link
Contributor Author

JoshuaKGoldberg commented May 21, 2020

I'd be fine dropping this PR & letting you / someone else pick it up.

I was planning on spending some time to really go through and understand the constraints & reportings here, which are a good bit deeper than I realized going in... it'd be better IMO if the PR gets merged before I do that (and I have other PRs that are actually ready for review) 😄.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 22, 2020

I'll take it on

@DanielRosenwasser DanielRosenwasser requested a review from sandersn May 22, 2020
PR Backlog automation moved this from Waiting on author to Needs merge May 22, 2020
@@ -32,7 +32,7 @@ tests/cases/compiler/extractInferenceImprovement.ts(28,26): error TS2345: Argume
// Should fail
prop = getProperty2(obj, s);
~
!!! error TS2345: Argument of type 'unique symbol' is not assignable to parameter of type 'never'.
!!! error TS2345: Argument of type 'typeof s' is not assignable to parameter of type 'never'.
Copy link
Member

@DanielRosenwasser DanielRosenwasser May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, this one definitely feels arguable. Not sure if @rbuckton has strong feelings about it, but I don't think it's a blocker.

@DanielRosenwasser DanielRosenwasser merged commit f2d6eda into microsoft:master May 26, 2020
2 checks passed
PR Backlog automation moved this from Needs merge to Done May 26, 2020
@JoshuaKGoldberg JoshuaKGoldberg deleted the literal-to-primitive-relation-reporting branch Aug 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
PR Backlog
  
Done
Development

Successfully merging this pull request may close these issues.

Unexpected type listed in error message
4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.