GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Weak type detection by RyanCavanaugh · Pull Request #3842 · microsoft/TypeScript

Conversation

@RyanCavanaugh

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) commented Jul 13, 2015

Copy link
Copy Markdown
Member

This implements the other suggestion from Anders Hejlsberg (@ahejlsberg) in #3755, and fixes a compiler bug found by the rule (in editorServices.ts).

A weak type is one which has only optional properties and is not empty. Because these types are assignable from anything except types with matching non-assignable properties, they are very weakly typechecked. The simple fix here is to require that a type is only assignable to a weak type if they are not completely disjoint.

Example:

interface TextOptions {
    alignment?: string;
    color?: string;
    padding?: number;
}
function drawText(opts: TextOptions) { ... }

// All of these are errors under this PR

// Also error under #3823
drawText({ align: 'center'} ); 

// Also error under #3823
function getDefaultOptions(): TextOptions {
    return { colour: 'blue'; };
}

// Not covered by #3823, but is covered in this PR
drawText(getDefaultOptions);
// Not covered by #3823, but is covered in this PR
drawText(getDefaultOptions());
// Not covered by #3823, but is covered in this PR
drawText(42);

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Bobris reacted with heart emoji
All reactions
  • ❤️ 1 reaction
Ryan Cavanaugh (RyanCavanaugh) added 2 commits July 9, 2015 21:44
@RyanCavanaugh
Implementation for weak types
bdcc4eb
@RyanCavanaugh
Baselines for weak types
3c76c3e
@msftclas Microsoft Contribution License Agreements (msftclas) added the cla-not-required label Jul 13, 2015
@danquirk

Dan Quirk (danquirk) commented Jul 13, 2015

Copy link
Copy Markdown
Member

Note this would be a breaking change.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JsonFreeman

Jason Freeman (JsonFreeman) commented Jul 14, 2015

Copy link
Copy Markdown
Contributor

👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Aug 6, 2015

Copy link
Copy Markdown
Contributor

Ryan Cavanaugh (@RyanCavanaugh) can we close this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Aug 6, 2015

Copy link
Copy Markdown
Member Author

Need to discuss at a design meeting to get a 👍 or 👎 on this

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@danquirk Dan Quirk (danquirk) mentioned this pull request Aug 26, 2015
Object strict literal assigment checking is omitted for the empty type #4463
Closed
@xLama

JCLama (xLama) commented Aug 26, 2015

Copy link
Copy Markdown

What about this?

interface TextOptions {
    alignment?: string;
    color?: string;
    padding?: number;
    length?: number;
}
function drawText(opts: TextOptions) { ... }
drawText( [] );

Would it show an error?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Aug 27, 2015

Copy link
Copy Markdown
Member Author

JCLama (@xLama) that would not be an error due to the corresponding length properties in both types.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xLama

JCLama (xLama) commented Aug 27, 2015

Copy link
Copy Markdown

👍

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xLama

JCLama (xLama) commented Sep 2, 2015

Copy link
Copy Markdown

Will this be in 1.6?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rotemdan

Rotem Dan (rotemdan) commented Sep 3, 2015

Copy link
Copy Markdown

Just encountered this:

let intendedToBeANonPrimitive: { val1?: number, val2?: string };
intendedToBeANonPrimitive = 45.234234; // Works?!

If the intention of the programmer was to allow the value to be assignable from a primitive, they would never have defined properties on it, either optional or required. If that was actually desirable, it is possible to use a union:

let canAlsoBeANumber: { val1?: number, val2?: string } | number;
canAlsoBeANumber= 45.234234; // OK

I encounter this problem very frequently with function "options" arguments, which are commonly defined to have all-optional properties.

I frequently convert an old API that looks something like this:

function getValues(startIndex?: number, endIndex?: number)

To a new one accepting an options object instead:

function getValues(options: { startIndex?: number, endIndex?: number })

Then unexpectedly I don't get any error on incorrect calls like these:

getValues(456);

And have to track them manually.

It would be great to have this fixed already in the 1.6 release. I personally consider this to be a bug. Anyway, keep up the great work :).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Sep 4, 2015

Copy link
Copy Markdown
Member

That issue surfaces pretty badly when .d.ts authors order overloads incorrectly and order an overload taking a string after an overload taking an options bag.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@JsonFreeman

Jason Freeman (JsonFreeman) commented Sep 5, 2015

Copy link
Copy Markdown
Contributor

Daniel Rosenwasser (@DanielRosenwasser) While I agree it's a bad problem, I don't think the overload ordering makes it that much worse because the initial pass of overload resolution uses subtype, not assignability. Subtype is tighter than assignability, and actually requires optional properties in the target to be present in the source, except when the source is an object literal. So a primitive, when passed to a function that expects an options bag, would fail subtype.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rotemdan Rotem Dan (rotemdan) mentioned this pull request Sep 21, 2015
Expanded type guard suggestions #4868
Closed
@DanielRosenwasser

Daniel Rosenwasser (DanielRosenwasser) commented Sep 28, 2015

Copy link
Copy Markdown
Member

One temporary workaround I gave a user was the following

interface NoPrimitives {
    valueOf?: { __DoNotUse: {} }; // Incompatible with 'valueOf' in strings, numbers, and booleans
    test?: { __DoNotUse: {} }; // Incompatible with 'test' in regexes
}

 interface Schema extends NoPrimitives {
  type?: string;
  items?: Schema | Schema[];
  properties?: {[property: string]: Schema};
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Oct 14, 2015
Weird behaviour with union types and 'strict' object literals #5237
Closed
@mhegazy Mohamed Hegazy (mhegazy) mentioned this pull request Dec 1, 2015
Type guards don't work for interfaces with only optional properties #5842
Closed
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) closed this Dec 7, 2015
@danquirk Dan Quirk (danquirk) mentioned this pull request Mar 11, 2016
All Optional Object Interface Converts to any #7485
Closed
@kitsonk

Kitson Kelly (kitsonk) commented Mar 11, 2016

Copy link
Copy Markdown
Contributor

This appears to have been closed without merging (or at least it doesn't appear to be working re: #7485). Was that intended?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Mar 12, 2016

Copy link
Copy Markdown
Contributor

the issue still remains. the proposal is still under consideration, but there were no immediate plans to pursue it, that is why it is closed.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kitsonk

Kitson Kelly (kitsonk) commented Mar 12, 2016

Copy link
Copy Markdown
Contributor

Is there an issue with a proposal that is open?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mhegazy

Mohamed Hegazy (mhegazy) commented Mar 13, 2016

Copy link
Copy Markdown
Contributor

Not that i am aware of. Ryan Cavanaugh (@RyanCavanaugh) ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@RyanCavanaugh

Ryan Cavanaugh (RyanCavanaugh) commented Mar 14, 2016

Copy link
Copy Markdown
Member Author

Nope.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@LittleBrainz Stephen Turner (LittleBrainz) mentioned this pull request Mar 18, 2016
IntelliSense / JSDoc - function declaration not same as function expression #7586
Closed
This was referenced Mar 23, 2016
allow string literals (unions of string literals?) as a type of an indexer #7656
Closed
Index signature arguments are not type checked based on the specialized key type they are constrained to #7660
Closed
@malibuzios malibuzios mentioned this pull request May 13, 2016
Type mismatch #8590
Closed
@sandersn Nathan Shively-Sanders (sandersn) mentioned this pull request Jul 20, 2016
Interfaces with all members optional is almost the same as 'any' #9841
Closed
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Sep 16, 2016
Partial Types (Optionalized Properties for Existing Types) #4889
Closed
@DmitryEfimenko Dmitry A. Efimenko (DmitryEfimenko) mentioned this pull request Dec 15, 2016
Two types should only be compatible if their members are compatible - even if all these members are optional #12904
Closed
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) mentioned this pull request Mar 7, 2017
Exact Types #12936
Open
@sandersn

Nathan Shively-Sanders (sandersn) commented May 19, 2017

Copy link
Copy Markdown
Member

I merged master with this PR and immediately found an incorrect argument being passed from checker. I'll create a new PR based on a branch in Microsoft/TypeScript when I get all the bugs fixed and tests passing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn

Nathan Shively-Sanders (sandersn) commented May 22, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

I found an additional case that we may not want to report errors for:

class A { a? }
declare function foo(a: A): void;
function bar(faux: Object) {
  foo(faux) // Error: Object doesn't have any properties in common with weak type A
}

The author should have used faux: {} to avoid this error, but lots of our users don't know that.

The real example is more believable because foo is a type guard:

class A { a? }
class B extends A { b }
declare function isB(a: A): a is B;
function bar(faux: Object) {
  if (isB(faux)) { // Error: Object doesn't have any properties in common with weak type A
    // sad code ... should have said `faux: A` or `isB(a: any)`
  }
}
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn Nathan Shively-Sanders (sandersn) mentioned this pull request May 23, 2017
Weak type detection #16047
Merged
@microsoft Microsoft (microsoft) locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

10 participants

@RyanCavanaugh @danquirk @JsonFreeman @mhegazy @xLama @rotemdan @DanielRosenwasser @kitsonk @sandersn @msftclas
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.