Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Weak types must not have call or construct sigs
This changes the definition of weak types
  • Loading branch information
sandersn committed May 26, 2017
commit 548f92ad3487c63add24ee0bc2096382055a9403
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9389,13 +9389,15 @@ namespace ts {

/**
* A type is 'weak' if it is an object type with at least one optional property
* and no required properties or index signatures
* and no required properties, call/construct signatures or index signatures
*/
function isWeak(type: Type) {
const props = getPropertiesOfType(type);
return type.flags & TypeFlags.Object &&
props.length > 0 &&
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could you rename as isWeakType?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

every(props, p => !!(p.flags & SymbolFlags.Optional)) &&
!getSignaturesOfType(type, SignatureKind.Call).length &&
!getSignaturesOfType(type, SignatureKind.Construct).length &&
!getIndexTypeOfType(type, IndexKind.String) &&
!getIndexTypeOfType(type, IndexKind.Number);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/weakType.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@ tests/cases/compiler/weakType.ts(31,18): error TS2345: Argument of type '{ error
!!! error TS2345: Argument of type '{ error?: number; }' is not assignable to parameter of type 'ChangeOptions'.
!!! error TS2345: Weak type 'ChangeOptions' has no properties in common with '{ error?: number; }'.
}

class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K

16 changes: 16 additions & 0 deletions tests/baselines/reference/weakType.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ function del(options: ConfigurableStartEnd = {},
changes.push(options);
changes.push(error);
}

class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K


//// [weakType.js]
Expand All @@ -48,3 +58,9 @@ function del(options, error) {
changes.push(options);
changes.push(error);
}
var K = (function () {
function K(s) {
}
return K;
}());
var ctor = K;
10 changes: 10 additions & 0 deletions tests/cases/compiler/weakType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,13 @@ function del(options: ConfigurableStartEnd = {},
changes.push(options);
changes.push(error);
}

class K {
constructor(s: string) { }
}
// Ctor isn't a weak type because it has a construct signature
interface Ctor {
new (s: string): K
n?: number
}
let ctor: Ctor = K