Commit graph

2347 commits

Author SHA1 Message Date
Andrew Branch 9a3149b967
Add failing tests for destructuring void 2019-04-01 13:33:41 -07:00
Nathan Shively-Sanders 32054f1c31
Forbid accessing block-scoped variables on globalThis (#30510)
* Forbid accessing const & let on globalThis

It's just an error; you still get the type of the property.

* Disallow access of blockscoped vars on globalThis

Also change Array, Function, String, et al from `const` to `var` so that
they remain accessible via `globalThis.String`.

* Update baselines after lib.d.ts change

Note especially the change in redefineArray, which is now allowed as
long as you provide a type that is assignable to ArrayConstructor.

* Remove blockscoped vars from typeof globalThis

Unlike forbidding them, this removes the properties entirely.

Unfortunately, this means that accessing these properties is only an
error with noImplicitAny, and that error is quite confusing.

Let's discuss our options. I see 3:

1. Forbid access of block-scoped vars as properties (in all flag
settings), but leave them on the type. Simple to implement.
2. Remove block-scoped vars from the globalThis type. Has the bad
error/flag behaviour described above, but simple to implement.
3. Remove block-scoped vars from the globalThis type. Also, forbid
accessing them by executing another resolveName lookup for failed
property accesses on globalThisSymbol. If the second lookup returns a
blockscoped var, issue an error instead of falling back to the index
signature. This seems too complex to me.

* Update baselines

* Better error for block-scoped usage on globalThis

So that value-space references have as clear an error as type-space
references.

* Update fourslash tests

* Fix semi-colon lint

* Don't copy so much when filtering blockscoped vars
2019-03-25 14:07:48 -07:00
王文璐 35ded510bb Merge branch 'master' into attach_property_to_default_export 2019-03-22 17:40:37 +08:00
Ron Buckton 2932421370
Merge pull request #30495 from Microsoft/fix29427
Adjust offset to account for 'this' parameter when emitting parameter decorators
2019-03-20 12:56:50 -07:00
Ron Buckton 07bec2893f Adjust offset to account for 'this' parameter when emitting parameter decorators 2019-03-19 11:46:18 -07:00
Ron Buckton e19c7f1a45 Prevent substitution of 'super' in async super helper 2019-03-19 11:35:18 -07:00
Ron Buckton 4e54f30fb4 Fix _superIndex emit when super access captured in async arrow 2019-03-19 10:20:08 -07:00
Collins Abitekaniza 7b55d1846b Giving too many arguments should error on the first argument that exceeds arity (#27982)
* span on first arg that exceeds arity

* refactor baseline

* handle cases for spread arguments

* refactor + add coverage for tuple spread cases

* create diagnostic on NodeArray of exceeding args

* test function overloading
2019-03-12 15:57:12 -07:00
Wenlu Wang d2476759e2 add related error span for default exports (#25396)
* add related error span for default exports

* accept baseline

* stash

* accept baseline and fix lint

* update testcase

* Add missing semicolon
2019-03-12 13:15:14 -07:00
Wenlu Wang bd27296ba6 improve stripInternal with inline comments (#23611)
* improve stripInternal with inline comments

* fix lint

* stash

* simptify StripInternal

* fix internal type declaration

* fix internal type declaration again

* accept baseline

* refactor inline

* simply prev check

* remove getTrailingCommentRangesOfNode

* Merge implementation with new isInternalDeclaration method, accept lkg-based baseline
2019-03-12 13:14:47 -07:00
Jack Williams 5bef1aa13c Add regressions for conditional types that affect parameter variance (#30146) 2019-02-28 16:14:16 -08:00
Anders Hejlsberg 7f5052bf7b
Merge pull request #30114 from Microsoft/contextualGenericRestParameter
Improve contextual typing by generic rest parameters
2019-02-28 11:00:12 -10:00
Anders Hejlsberg 237c33b444
Merge pull request #30109 from Microsoft/circularConstraintErrors
Consistently error on circular constraints
2019-02-28 10:57:01 -10:00
Nathan Shively-Sanders be2db9db12
Add globalThis (#29332)
* Restore original code from bind-toplevel-this

With one or two additional comments

* Working in JS, but the symbol is not right.

Still need to

1. Make it work in Typescript.
2. Add test (and make them work) for the other uses of GlobalThis:
window, globalThis, etc.

* Check in TS also; update some tests

Lots of tests still fail, but all but 1 change so far has been correct.

* Update baselines

A couple of tests still fail and need to be fixed.

* Handle type references to globalThis

The type reference must be `typeof globalThis`. Just `globalThis` will
be treated as a value reference in type position -- an error.

* Restore former behaviour of implicitThis errors

I left the noImplicitThis rule for captured use of global this in an
arrow function, even though technically it isn't `any` any more --
it's typeof globalThis.  However, you should still use some other method
to access globals inside an arrow, because captured-global-this is super
confusing there.

* Test values with type globalThis

I ran into a problem with intersecting `Window & typeof globalThis`:

1. This adds a new index signature to Window, which is probably not
desired. In fact, with noImplicitAny, it's not desired on globalThis
either I think.
2. Adding this type requires editing TSJS-lib-generator, not this repo.

So I added the test cases and will probably update them later, when
those two problems are fixed.

* Add esnext declaration for globalThis

* Switch to symbol-based approach

I decided I didn't like the import-type-based approach.

Update baselines to reflect the difference.

* Do not suggest globals for completions at toplevel

* Add tests of element and property access

* Look up globalThis using normal resolution

globalThis is no longer constructed lazily. Its synthetic Identifier
node is also now more realistic.

* Update fourslash tests

* Add missed fourslash test update

* Remove esnext.globalthis.d.ts too

* Add chained globalThis self-lookup test

* Attempt at making globalThis readonly

In progress, had to interrupt for other work.

* Add/update tests

* Addres PR comments:

1. Add parameter to tryGetThisTypeAt to exclude globalThis.
2. Use combined Module flag instead combining them in-place.
3. SymbolDisplay doesn't print 'module globalThis' for this expressions
anymore.
2019-02-27 14:14:34 -08:00
Nathan Shively-Sanders aedffe049d Revert "Merge pull request #27697 from mattmccutchen/issue-27118"
This reverts commit 2dfb6202ed, reversing
changes made to bbf559b9c7.
2019-02-27 11:50:04 -08:00
Anders Hejlsberg 2212f4777a Add regression test 2019-02-26 12:44:12 -08:00
Anders Hejlsberg f19191b081 Add tests 2019-02-19 07:02:50 -10:00
Anders Hejlsberg f93f4f3c62
Merge pull request #29847 from Microsoft/inferToUnionTypes
Improve inference to union and intersection types
2019-02-11 11:34:05 -08:00
Anders Hejlsberg d66000bc47
Merge pull request #29787 from Microsoft/inferToPartiallyHomomorphic
Infer to partially homomorphic types (such as Pick<T, K>)
2019-02-11 11:33:27 -08:00
Anders Hejlsberg ce6c04e9b5 Merge branch 'master' into inferToUnionTypes 2019-02-11 11:00:41 -08:00
Anders Hejlsberg 32b44ac65a
Merge pull request #29858 from Microsoft/fixContextualRestTuple
Fix contextual types computed from rest parameters
2019-02-11 10:59:11 -08:00
Anders Hejlsberg 710826e37e Add regression test 2019-02-11 09:46:02 -08:00
Anders Hejlsberg 35cf397ae3 Add regression tests 2019-02-10 15:29:14 -08:00
Matt McCutchen 1aca1dd036 Make the assignability rule for conditional types require the check
types and distributivity to be identical.

Fixes #27118.
2019-02-09 17:22:08 -05:00
Anders Hejlsberg e49320d1db Add more tests 2019-02-08 06:49:26 -08:00
Kagami Sascha Rosylight a8823f5169
Merge branch 'master' into es2019 2019-02-07 10:08:26 +09:00
Anders Hejlsberg 62c62f4f87 Add tests 2019-02-06 15:41:43 -08:00
Kagami Sascha Rosylight 6249b4c704 Remove ES2018 transforms for ES2018 target (#26315)
* rename esnext.ts as es2018.ts

* remove ES2018 transforms for ES2018 target

* change target from esnext to es2018

* rename tests
2019-02-06 15:35:52 -08:00
Anders Hejlsberg d9ee86723a
Merge pull request #29740 from Microsoft/fixCircularMappedArrayTuple
Fix issues related to circular mapped array and tuple types
2019-02-06 13:24:19 -08:00
Anders Hejlsberg 7c096576bc
Merge pull request #29756 from Microsoft/fixStrictPropertyInitialization
Revert change to strict property initialization checks
2019-02-05 15:10:30 -08:00
Anders Hejlsberg 2d44a40202 Add regression test 2019-02-05 13:08:18 -08:00
Ryan Cavanaugh 1ec8a71949
Merge pull request #29711 from jack-williams/switch-on-unknown
Fix #29710: Narrow unknown in switch
2019-02-05 09:08:52 -08:00
Anders Hejlsberg bee9f1a5e7 Add regression tests 2019-02-04 17:48:53 -08:00
Ron Buckton 55762271af
Merge pull request #29695 from Microsoft/fixDtsTypeReferenceEmit
Fix type reference dts emit failure
2019-02-04 13:03:07 -08:00
Jack Williams 3d0c7f3156 Narrow unknown in switch 2019-02-03 16:07:21 +00:00
Anders Hejlsberg 332861b4fc Add tests 2019-02-02 09:37:52 -08:00
Ron Buckton 4b84f56b21 Fix type reference dts emit failure 2019-02-01 16:09:17 -08:00
Klaus Meinhardt 782622f9cd clarify error message for 'readonly' type operator 2019-01-31 19:44:49 +01:00
Klaus Meinhardt f4747f4667 Merge branch 'master' of github.com:Microsoft/TypeScript into add-to-types2 2019-01-31 11:25:01 +01:00
Anders Hejlsberg 96706a75ed
Merge pull request #29510 from Microsoft/constContexts
Const contexts for literal expressions
2019-01-30 15:55:47 -08:00
Ryan Cavanaugh 0df89cc96f
Merge pull request #26941 from jack-williams/narrow-unknown-with-triple-equals
Fix #25172: Add narrowing for `unknown` with triple equals
2019-01-29 16:43:50 -08:00
Anders Hejlsberg 08fe06f527 Merge branch 'master' into constContexts
# Conflicts:
#	src/compiler/checker.ts
2019-01-29 12:29:30 -08:00
Anders Hejlsberg e290559057 Add tests for decorators and declaration emit from error free source 2019-01-26 14:57:02 -08:00
Anders Hejlsberg 4706a060a5 Merge branch 'master' into readonlyArrayTuple 2019-01-26 14:44:15 -08:00
Klaus Meinhardt 6eb83650ea Merge branch 'master' into add-to-types2 2019-01-25 16:56:11 +01:00
Jack Williams 36cc154985 Narrow to any primitive or object and update tests 2019-01-25 09:41:34 +00:00
Jack Williams 1f2338bd78 Update test file and baselines 2019-01-25 09:41:34 +00:00
Jack Williams 267e5989cd Add narrowing for unknown with strict equal 2019-01-25 09:41:34 +00:00
Ryan Cavanaugh 16b6706ee1
Merge pull request #29463 from jack-williams/check-in-expression-with-union
Fix #29457
2019-01-24 13:07:22 -08:00
Anders Hejlsberg 2e94f47602 Add tests 2019-01-19 10:34:38 -08:00
Pranav Senthilnathan d38c616e29
Fix resolution of properties from prototype assignment in JS (#29302)
* fix type derived from prototype assignment

* accept new baselines

* remove direct intersection with object literal assigned to prototype

* add tests

* change webpack submodule commit

* fix submodule commits

* comment and simplify getJSDocTypeReference

* remove circularity guards that aren't hit anymore
2019-01-18 16:00:18 -08:00
Anders Hejlsberg 2200d35385 Add tests 2019-01-18 09:34:38 -08:00
Jack Williams 5651789629 Fix #29457
Use allTypesAssignableToKind instead of isTypeAssignableToKind to
account for union types.
2019-01-17 20:03:46 +00:00
Wesley Wigham 045a450972
Merge branch 'fix-generic-types-intersections' of git://github.com/flapenguin/TypeScript into flapenguin-fix-generic-types-intersections 2019-01-17 10:49:04 -08:00
Anders Hejlsberg 0eac506fc0 Add .d.ts generation test 2019-01-16 12:45:33 -08:00
Anders Hejlsberg 2ccc106984 Add more tests 2019-01-16 09:46:28 -08:00
Wesley Wigham 41a7bf4b73
Fake up value declaration for synthetic jsx children symbol so they get excess property checked (#29359) 2019-01-15 22:31:57 -08:00
Kagami Sascha Rosylight d8ac9ba414
Merge branch 'master' into es2019 2019-01-16 10:54:46 +09:00
Anders Hejlsberg cff7874288 Add tests 2019-01-15 14:44:57 -08:00
Ron Buckton 5763e2c3d4 Remove overzealous simple relationship check for unique symbols 2019-01-14 17:08:04 -08:00
Gabriela Britto e5708e1903
Merge pull request #29352 from Microsoft/qualified-name-param-tag-error
Qualified name param tag error
2019-01-14 10:28:08 -08:00
Martin Probst f3f5877c5f Add tests for noLib with <reference lib> and bundling. 2019-01-11 09:02:49 +01:00
Martin Probst cc7ddaed28 Add tests for noLib with <reference lib>. 2019-01-11 09:02:49 +01:00
Gabriela Britto b3633fab52 Add more tests for qualified name param without top level object error 2019-01-10 15:04:16 -08:00
Gabriela Britto ebe193c6d7 Minor refactor in paramTagNestedWithoutTopLevelObject.ts 2019-01-10 14:05:10 -08:00
Gabriela Britto e2524e3750 Add test for qualified name param without top level object error 2019-01-10 09:55:06 -08:00
Anders Hejlsberg 9fda7014ca Add regression tests 2019-01-09 16:16:24 -08:00
王文璐 34b1384152 add transformer for emit add property to default export 2019-01-03 11:19:02 +08:00
Kagami Sascha Rosylight c89a80736e add ES2019 target 2018-12-27 19:46:21 +09:00
Anders Hejlsberg 7ad2661625 Add tests 2018-12-21 12:51:01 -08:00
Anders Hejlsberg 3e0639add5
Merge pull request #29053 from Microsoft/fixDestructuringControlFlow
Fix destructuring control flow analysis
2018-12-19 11:12:41 -08:00
Anders Hejlsberg 519c501ea5 Add regression test 2018-12-18 11:58:06 -08:00
Anders Hejlsberg 307a9b66af Add tests 2018-12-16 07:23:28 -08:00
Anders Hejlsberg 77d01ab332 Update tests 2018-12-11 15:12:47 -08:00
Anders Hejlsberg 2150a64f0c Add tests 2018-12-11 12:11:16 -08:00
Anders Hejlsberg 45411e525c Add tests 2018-12-10 16:30:30 -08:00
Daniel Rosenwasser 3e6a666b5e
Merge pull request #28919 from a-tarasyuk/feature/28893-allow-trailing-comma-on-tuple-types
feature/28893 allow trailing comma on tuple types
2018-12-10 11:33:33 -08:00
Anders Hejlsberg cd6d2fdc18
Merge pull request #28940 from Microsoft/explicitUnknownConstraint
Type parameter constrained to 'unknown' not assignable to '{}'
2018-12-10 10:29:06 -08:00
Anders Hejlsberg d2cc282134 Add tests 2018-12-10 09:45:55 -08:00
Anders Hejlsberg 7a0779288f Add tests 2018-12-10 09:14:41 -08:00
Alexander e219b17ff0 TupleType - add additional tests to handle error on extra comma 2018-12-08 20:50:09 +02:00
Anders Hejlsberg 4ae0848bd4 Add tests 2018-12-05 15:20:21 -08:00
Anders Hejlsberg 3e2f130b3c Add regression test 2018-12-04 13:18:04 -08:00
Anders Hejlsberg d832fbf46a Add regression test 2018-11-28 14:58:11 -08:00
Andrey Roenko bea7ed1aa7 Merge branch 'master' into fix-generic-types-intersections 2018-11-20 20:31:02 +03:00
Wesley Wigham 96937fd592
Allow union signatures to merge when they have differing argument counts (#28604)
* Allow union signatures to merge when they have differing argument counts

* Accept updated baselines

* Adjust comments io changed tests
2018-11-19 17:05:28 -08:00
Nathan Shively-Sanders 0774bb81ce
Fix crash on property assignment of unresolved module (#28606)
Previously, the compiler would crash when binding a non-top-level
property assignment on the symbol of an unresolved module:

```js
import x from 'arglebaz'
{
    x.bar = 1
}
```

That's because `x` looks like an alias but doesn't have a
valueDeclaration (since there is no file named 'arglebaz'), and the new
code for binding Object.defineProperty calls forgot to check for an
undefined valueDeclaration.

This change adds the checks for an undefined valueDeclaration.
2018-11-19 13:29:46 -08:00
Andy e0dca0bd95
Error when destructuring private property in a parameter (#28562) 2018-11-16 12:47:05 -08:00
Nathan Shively-Sanders 53bb4e84a2
Better checking of assignment declarations (#28387)
Previously, type checking was turned off for all assignment
declarations. This is a problem when the declarations are annotated with
jsdoc types.

This PR checks assignment declarations, *except* for expando
initialisers. Expando initialisers are

1. Empty object types.
2. Function types.
3. Class types.
4. Non-empty object types when the assignment declaration kind is
prototype assignment or module.exports assignment.
2018-11-15 08:46:11 -08:00
Anders Hejlsberg 627f1ad876 Add regression tests 2018-11-09 06:45:21 +01:00
Anders Hejlsberg 48c0aedf54 Add tests 2018-11-02 08:20:06 -07:00
Anders Hejlsberg 8e4b90da00
Merge pull request #28234 from Microsoft/genericSpread
Generic spread expressions in object literals
2018-10-31 12:52:16 -07:00
Anders Hejlsberg 2ed627aaf1 Update tests 2018-10-30 16:33:14 -07:00
Klaus Meinhardt 1397fed2ad Only suggest adding to types if present in compilerOptions
Fixes: https://github.com/Microsoft/TypeScript/pull/28211#issuecomment-434438407
2018-10-30 21:31:22 +01:00
Anders Hejlsberg e0d210d027 Add tests 2018-10-30 08:18:18 -07:00
Nathan Shively-Sanders 64ff195426
Set-only accessors spread to undefined (#28213)
* Set-only accessors spread to undefined

Previously they were skipped. The runtime behaviour is to create a
property of type undefined, unlike (for example) spreading numbers or
other primitives. So now spreading a set-only accessor creates a
property of type undefined:

```ts
const o: { foo: undefined } = { ...{ set foo(v: number) { } } }
```

Notably, `o.foo: undefined` not `number`.

Fixes #26337

* Fix isSpreadableProperty oversimplification
2018-10-29 14:51:12 -07:00
Anders Hejlsberg ccc16136b2
Merge pull request #28170 from Microsoft/fixGenericMappedTypeConstraint
No constraint for { [P in K]: XXX } where K is type variable
2018-10-26 16:02:17 -07:00
Anders Hejlsberg 24e3745296 Add regression test 2018-10-26 15:32:10 -07:00
Andy 36dfd775b3
Parse an object literal property as shorthand unless followed by '(' or ':' (#28121) 2018-10-26 15:00:31 -07:00
Nathan Shively-Sanders dc9a066f65
Do not merge commonJS exports into an alias (#28133)
* Do not merge commonsjs exports onto an alias

getCommonJSExportEquals merges export assignments and export property
assignments. Something like this, which has no equivalent structure in
TS:

```js
module.exports = function() { }
module.exports.expando = 1
```

However, it is sometimes called with an alias, when its
parent, resolveExternalModuleSymbol, is called with dontResolveAlias:
true, and when the initialiser of the export assignment is an alias:

```js
function alias() { }
module.exports = alias
module.exports.expando = 1
```

In this case, (1) the actual value `alias` will have already merged in a
previous call to getCommonJSExportEquals and
(2) getTypeOfSymbol will follow the alias symbol to get the right type.
So getCommonJSExportEquals should do nothing in this case.

This bug manifests in the code for dynamic imports, which calls
getTypeOfSymbol on the incorrectly merged alias, which now has enough
value flags--Function, for example--to take the wrong branch and
subsequently crash.

* Update baselines
2018-10-25 15:08:06 -07:00
Wesley Wigham 0c36266706
Obey the excludeArgument parameter when checking JSX signature validity (#28002)
* Obey the excludeArgument parameter when checking JSX signature validity

* Fix conditional type extending any contextual types and accept baselines

* use flag check to also drop unknown from comparison for the same reason

* Slight refinement - make an intersection to ensure parameter constraints flow through contextual types when instantiated

* Format ternary more nicely
2018-10-22 16:36:11 -07:00
Wesley Wigham f701daf4e0
Infer over each mapped type constraint member if it is a union (#28006) 2018-10-22 16:33:43 -07:00
Nathan Shively-Sanders bf393ae1cd
Check EOF token to get errors for JSDoc (#28000)
* Check EOF token to get errors for JSDoc

* outputFile instead of noEmit for test
2018-10-19 16:23:34 -07:00
Wesley Wigham 69b1cb5bac
Add new special assignment kinds for recognizing Object.defineProperty calls (#27208)
* Add new special assignment kinds for recognizing Object.defineProperty calls

* Add support for prototype assignments, fix nits

* Fix code review comments

* Add test documenting behavior in a few more odd scenarios
2018-10-19 14:31:55 -07:00
Nathan Shively-Sanders e379aeb151
Fix alias of module.exports->exports->IIFE (#27992)
In JS, when you assign `module.exports = exports` and the entire module is
wrapped in an IIFE, the resulting `export=` symbol, after following
aliases, is the module itself. This results in trying to merge the
file's exports with itself inside `getCommonJsExportEquals`, since it
thinks that it has found new exports, possibly in an object literal,
that need to be merged with the file's exports.

For example:

```js
(function() {
exports.a = 1
module.exports = exports
})()
```
2018-10-19 13:50:38 -07:00
Ryan Cavanaugh b64d08a21b
Merge pull request #27522 from jack-williams/trailing-void-args-are-optional
Fix #4260 : Allow trailing arguments that accept void to be omitted
2018-10-19 10:16:56 -07:00
Nathan Shively-Sanders 8779d4ca88
Fix JS merge crashes from lovefield (#27989)
1. Merge enum with expando.
2. Merge enum member with property assignment.
3. Merge interface-declared method declaration with
prototype-property-assignment method declaration.

The reason that the enum merges crash is that getTypeOfSymbol assumes
that symbol flags are (basically) mutually exclusive. This assumption is
shredded, badly, for JS merges.

One fix is to drop the assumption of exclusivity and instead order cases
by least to most likely. This has the highest chance of working, but is
also slow, since you would prefer to order cases by most likely *first*,
not *last*.

The other fix, which is what I did here, is to add a last-chance
re-dispatch at the bottom of
`getTypeOfVariableOrParameterOrPropertyWorker`. This dispatch uses the
valueDeclaration instead of the symbol flags.
2018-10-19 09:23:05 -07:00
Jack Williams 8500f7ce20 Merge branch 'master' into trailing-void-args-are-optional 2018-10-18 19:22:18 +01:00
Wesley Wigham 7b5ef64e76
Unify JSX And Normal Call Checking Codepaths (#27627)
* Unify JSX Call Checking Codepaths

* Add tests for fixed issues

* Fix lint, move all error checking into the only-run-once resolveSignature call

* Remove unused (unreachable?) code path

* Consolidate a little more duplicated logic into signature checking

* Fix #19775 a bit more

* Cosmetic changes from CR
2018-10-16 20:16:00 -04:00
Anders Hejlsberg 04fd365ec3
Merge pull request #27911 from Microsoft/fixCircularMappedType
Fix circular mapped type instantiations for arrays and tuples
2018-10-15 15:51:31 -07:00
Anders Hejlsberg 9767522ca0
Merge pull request #27695 from Microsoft/mixedDiscriminantTypes
Allow non-unit types in union discriminants
2018-10-15 13:10:27 -07:00
Nathan Shively-Sanders c184184713
Add getEffectiveConstructSignatures (#27561)
* Add helpers that understand constructor functions

* getEffectiveConstructSignatures gets construct signatures from type, and
  call signatures from constructor functions if there are no construct
  signatures.
* getEffectiveConstructSignatureReturnType gets the "JS Class type" for
  constructor functions, and the return type of signatures for all other
  declarations.

This is a first step toward making constructor functions have construct
signatures instead of call signatures, which will also contribute to
fixing instantiation of generic constructor functions, which is basically
broken right now.

Note that the baselines *improve* but, because of the previously
mentioned generic problem, are still not correct. Construct signatures
for constructor functions and generic constructor functions turns out to
be an intertwined problem.

* Correct correct originalBaseType

And, for now, return anyType for generic constructor functions used as
base types. Don't give an incorrect error based on the function's return
type, which is usually void.

* Add error examples to tests

* Add construct signatures instead of getEffective* functions

* Fix typo in baseline

* Remove pesky newline

I thought I got rid of it!

* Test of constructor tag on object literal method

It doesn't work, and shouldn't in the future, because it's a runtime
error.
2018-10-15 12:47:57 -07:00
Anders Hejlsberg 0c3221c220 Add regression test 2018-10-15 10:24:00 -07:00
Andrey Roenko 15b4af63dd #27716: fix protected methods for intersection fo generic classes 2018-10-12 18:23:07 +03:00
Sheetal Nandi c0729a22fd Use string/number signature to get contextual type
Fixes #26587
2018-10-11 15:12:13 -07:00
Anders Hejlsberg 5a126e2b27
Merge pull request #27587 from Microsoft/fixUnionOfTupleIndexing
Fix indexing and destructuring of unions of tuple types
2018-10-11 12:01:24 -07:00
Anders Hejlsberg 22907bfb07 Add tests 2018-10-10 15:40:12 -07:00
Anders Hejlsberg 209f30c2f1 Update test 2018-10-08 16:46:45 -07:00
Nathan Shively-Sanders ca840ee683 Fix name resolution of exports in JS (#27394)
The ad-hoc name resolution rule for `exports` forgets to check the
requested meaning. When `getTypeReferenceType` calls`
resolveTypeReferenceName` with `Type` only in order to give an error
when the program uses a value like a type, it is incorrectly able to
resolve `exports` instead of producing an error. Then this incorrect
symbol gets treated like an alias, which it isn't, causing the assert.

The fix, for now, is to make resolution of `exports` check the requested
meaning so that it only resolves when `Value` is requested. This makes
the above code an error ("Cannot use the namespace 'exports' as a
type."), but I think this is fine for a bug fix. We can decide later if
`exports` should behave like other expandos and be a legal type
reference.

Note that the name actually does resolve correctly, so JS users will get
the desired completions. They'll just have an error to suppress if they
have checkJs on.
2018-10-08 13:01:14 -07:00
Nathan Shively-Sanders 8a4b6e03ab Fix class/constructor-function merge (#27366)
The check for prototype assignment on constructor functions assumes
that the prototype property, if present, comes from an assignment
declaration, such as:

```js
SomeClass.prototype = { /* methods go here */ }
```

In this case, however, when class SomeClass and var SomeClass merge
(because this is allowed), prototype is the synthetic property from
class SomeClass, which has no valueDeclaration.

The fix is to check that prototype has a valueDeclaration before
checking whether the valueDeclaration is in fact a prototype-assignment
declaration.
2018-10-08 12:56:19 -07:00
Nathan Shively-Sanders b185784708 Only functions can be constructor functions (#27369)
`@constructor` put on anything incorrectly makes it a JS constructor. This
is a problem for actual constructors, because getJSClassType doesn't
work on actual classes. The fix is to make isJSConstructor require that
its declaration is a function.
2018-10-08 10:14:31 -07:00
Nathan Shively-Sanders a4a5b3806e Report circular JSDoc type references (#27404)
JSDoc types references can often be to values, which can often be
circular in ways that types tied to declarations cannot. I decided to
create a separate property on SymbolLinks rather than reusing
declaredType, although I'm not sure that's strictly required.
2018-10-08 08:56:29 -07:00
Anders Hejlsberg 62aeeadd1b Merge branch 'master' into fixUnionOfTupleIndexing
# Conflicts:
#	src/compiler/checker.ts
2018-10-06 18:21:23 -07:00
Anders Hejlsberg d4f480cbbb Add tests 2018-10-06 17:03:19 -07:00
Ron Buckton 85a3475df8
Merge pull request #26707 from mprobst/async-super-rename-safe
Per-property super accessors in async functions.
2018-10-05 17:20:21 -07:00
Wesley Wigham 07dbd8be21
Discriminate jsx contextual types same as object contextual types (#27408)
* Discriminate jsx contextual types same as object contextual types

* Extract core discrimination algorithm to getDiscriminationResultForProperty

* Merge all discrimination implementations

* Fix lints
2018-10-05 15:11:12 -07:00
Nathan Shively-Sanders c080324974
Elt access assignment uses declared, not narrowed type (#27574)
I forgot to do this in #26424.

Fixes #27557
Fixes #27412
2018-10-05 11:30:10 -07:00
Wesley Wigham e1d346ea53
Infer tuples for jsx children if contextually typed by a tuple (#27409) 2018-10-04 17:56:38 -07:00
Wesley Wigham f07404938f
Replace subtype check in derivedness check with flags and structure checks (#27403)
* Replace subtype check in derivedness check with flags and structure checks

* Remove now extraneous clause
2018-10-04 12:55:39 -07:00
Martin Probst 539c455942 Rename to _superIndex to test conflict
Change-Id: I30af09343446126ba73ed40199ecc3f0ed515b3e
2018-10-04 08:07:42 +02:00
Andy 9bdd6a3b55
Support loading "index.d.ts" using "typesVersions" without "types", "typings", or "main" (#27514)
* Support loading "index.d.ts" using "typesVersions" without "types", "typings", or "main"

* Update baseline
2018-10-03 11:44:16 -07:00
Jack Williams 342fda98d8 Allow trailing void arguments to be omitted 2018-10-03 19:35:05 +01:00
Anders Hejlsberg 9851d6f457
Merge pull request #27490 from Microsoft/indexedAccessConstraint
Limit unsound indexed access type relations
2018-10-03 10:58:45 -07:00
Martin Probst f0826cfeaa Per-property super accessors in async functions.
TypeScript must hoist accessors for super properties when converting
async method bodies to the `__awaiter` pattern for targets before
ES2016.

Previously, TypeScript would reify all property accesses into element
accesses, i.e. convert the property name into a string parameter and
pass it to `super[...]`. That breaks optimizers like Closure Compiler or
Uglify in advanced mode, when property renaming is enabled, as it mixes
quoted and un-quoted property access (`super['x']` vs just `x` at the
declaration site).

This change creates a variable `_superProps` that contains accessors for
each property accessed on super within the async method. This allows
accessing the properties by name (instead of quoted string), which fixes
the quoted/unquoted confusion. The change keeps the generic accessor for
element access statements to match quoting behaviour.

Fixes #21088.
2018-10-03 15:46:04 +02:00
Anders Hejlsberg 34994627f0 Add tests 2018-10-01 16:21:35 -07:00
Ryan Cavanaugh 3331d073c4
Merge pull request #26070 from ajafff/tuple-rest
Infer array rest as tuple if possible
2018-10-01 13:56:28 -07:00
Nathan Shively-Sanders 6d92a2942f
Fix parent points in unreachable code (#27400) (#27406)
In the binder, unreachable code mistakenly skips the `bindJSDoc` call in
`bindChildrenWorker`, which sets parent pointers. The fix is to call
`bindJSDoc` in the case of unreachable code as well.
2018-09-28 08:31:56 -07:00
Wesley Wigham 26eb6ab6f4
Primitives should not be instanceof... anything (#27402) 2018-09-27 15:49:51 -07:00
Anders Hejlsberg d7219b21c2
Merge pull request #27357 from Microsoft/fixBivariantInferences
Make contravariant inferences only from pure contravariant positions
2018-09-26 14:03:59 -07:00
Nathan Shively-Sanders 98ec1e8730
Fix commonjs export= merging (#27368) (#27371)
I'm surprised we haven't seen more of this; I suspect it's because the
mixed `module.exports=` + `export.foo=` pattern isn't that common.
However, it'll happen any time that the exported symbol is unknown;
getCommonJsExportEquals blithely clones unknownSymbol and proceeds to
stick the `exports.foo=` properties onto it.

This causes problems later, because the compiler checks for
unknownSymbol with `===`. The fix is to not stick properties onto a
clone of unknownSymbol. This makes the correct errors appear and removes
the crash.
2018-09-26 12:40:30 -07:00
Nathan Shively-Sanders 4fac5f26dc
Fix crash in use-before-def checking of enum tag (#27350) (#27354) 2018-09-26 09:05:18 -07:00
Anders Hejlsberg 4bb5cfb9bb Add regression test 2018-09-25 18:17:21 -07:00
Anders Hejlsberg 5510e0755e Merge branch 'master' into typedBindCallApply
# Conflicts:
#	tests/baselines/reference/tsxTypeArgumentPartialDefinitionStillErrors.errors.txt
#	tests/baselines/reference/wrappedAndRecursiveConstraints4.errors.txt
2018-09-24 16:38:39 -07:00
Ron Buckton 5fb39769ad
Merge pull request #27271 from Microsoft/fix24570-3
Fix iterated type in for-await-of
2018-09-21 12:28:18 -07:00
Ron Buckton 112fe6e2cc Fix iterated type in for-await-of 2018-09-21 10:40:45 -07:00
Ron Buckton 3a4d0b237f Add more tests for await 2018-09-21 10:32:28 -07:00
Ron Buckton 63adc5fb40 Add contextual typing for await operand 2018-09-21 10:32:18 -07:00
Ron Buckton 80dba4d63b Support promise-like types in contextual return type of async function 2018-09-21 10:32:00 -07:00
Nathan Shively-Sanders 90d3f8b573
Only report expando use-before-def for identical control flow containers (#27199) 2018-09-18 15:28:16 -07:00
Anders Hejlsberg c0eb742cf3 Merge branch 'master' into fixEmptyObjectFalsiness 2018-09-18 06:28:27 -07:00
Nathan Shively-Sanders 59e4770a51
Fix enum tag circular references (#27161)
* Fix enum tag circular references

Also, don't try to resolve enum tag types in Typescript.

* Improve comment
2018-09-17 16:06:17 -07:00
Nathan Shively-Sanders c9f190283e
Fix non-toplevel prototype assignment (#27096)
* Fix non-toplevel prototype assignment

binder was using the wrong node to lookup the containing class type for
prototype assignment, so it incorrectly put the prototype declaration on
the class' symbol.

This correction to the binder in turn required a change in
getJSClassType in the checker. It now has to look at the "prototype"
property for the prototype instead of looking on the class symbol's exports
(which makes no sense).

* Refactor per PR suggestion
2018-09-17 13:07:05 -07:00
Anders Hejlsberg eb06af1901 Add tests 2018-09-17 13:01:53 -07:00
Nathan Shively-Sanders 989a717b04
Definite assignment checking for expando properties (#27128) 2018-09-17 12:56:39 -07:00
Nathan Shively-Sanders 394ee31a56
Fix cross-file merge of assignment decl valueDeclaration (#26918)
* Fix cross-file merge of assignment decl valueDeclaration

Previously mergeSymbol in the checker always updated valueDeclaration if
target.valueDeclaration was an assignment declaration. The binder only
updates target.valueDeclaration if it is an assignment declaration and
source.valueDeclaration is *not* an assignment declaration. Now the
checker behaves the same way as the binder.

* Update baselines

* Add a fix for #27099

Makes commonjs merge with globals when appropriate.

* Add a separate jsGlobalAugmentations table

Instead of trying to filter these augmentations out of the normal symbol
table of commonjs modules.
2018-09-16 07:46:03 -07:00
Andy f71d6005a2
Use nextToken() after parsing a tag name so we can parse type keywords (#26915)
* Use nextToken() after parsing a tag name so we can parse type keywords

* Make callback to skipWhitespaceOrAsterisk non-optional
2018-09-13 15:49:06 -07:00
Nathan Shively-Sanders 614423b287
Fix this-type in prototype-assigned object literals (#26925)
* Fix this-type in prototype-assigned object literals

Some cases were missing from tryGetThisTypeAt.
Fixes #26831

* Lookup this in JS only for  @constructor+prototype assignments
2018-09-12 16:21:17 -07:00
Anders Hejlsberg b67a261eba Merge branch 'master' into typedBindCallApply
# Conflicts:
#	src/compiler/diagnosticMessages.json
2018-09-11 10:48:18 -07:00
Anders Hejlsberg e9679f0191 Add tests 2018-09-11 06:43:17 -07:00
Ron Buckton a255d9a163 Merge branch 'master' into typesVersions 2018-09-07 13:52:12 -07:00
Ryan Cavanaugh ed70d4887a
Merge pull request #25633 from Kingwl/strictParameter
add use strict and simple parameter check
2018-09-05 17:15:52 -07:00
Ryan Cavanaugh 1e2fb9f0ae
Merge pull request #26465 from rnathanday/master
include leading non-ASCII horizontal whitespace
2018-09-05 12:38:09 -07:00
Ryan Cavanaugh 6465e9dcdd
Merge pull request #26292 from Kingwl/tupleIndexAccessCheck
check index access for fixed length tuple
2018-09-05 12:10:46 -07:00
Ryan Cavanaugh 5d65e86756
Merge pull request #23253 from Kingwl/definite-assignment-assertion-improve
improve parser and error message if definite assignment assertions in…
2018-09-05 11:49:13 -07:00
Nathan Shively-Sanders ff05082e45
Bind non-expando property assignments at top-level (#26908)
* Bind non-expando property assignments at toplevel

Previously, only property assignments with expando initialisers were
bound in top-level statements. Now, all property assignments are bound.

This requires a matching change in the checker to make sure that these
assignments remain context sensitive if their valueDeclaration is a
'real' declaration (ie a non assignment-declaration).

* Add baselines for new test
2018-09-05 10:53:43 -07:00
Tim Schaub 262ea5b06e Skip asterisks after newline when parsing JSDoc types (#26528)
* Skip asterisks after newline when parsing JSDoc types

* Single boolean expression

* Test for parsing and printing multiline function signatures with *
2018-09-04 15:41:08 -07:00
Nathan Shively-Sanders 64ac5a53f4
Fixes for type parameter name resolution in JS (#26830)
* check for expando initializers in resolveEntityName

when resolving type parameters in a prototype property assignment
declaration. For example, this already works:

```js
/** @template T */
function f(x) { this.x = x }
/** @returns {T} */
f.protototype.m = function () { return this.x }
```

This now works too:

```js
/** @template T */
var f = function (x) { this.x = x }
/** @returns {T} */
f.prototype.m = function () { return this.x }
```

Fixes #26826

* Lookup type parameters on prototype-assignment methods

In the same way that they're looked up on prototype-property methods.

That is, this previously worked:

```js
/** @template T */
function f() { }
/** @param {T} p */
f.prototype.m = function () { }
```

And this now works too:

```js
/** @template T */
function f() { }
f.prototype = {
  /** @param {T} p */
  m() { }
}
```

Note that the baselines still have errors; I'll file a followup bug for
them.

* Look up types on property assignments too
2018-09-04 14:47:18 -07:00
Nathan Shively-Sanders c929e74310
Add [Constructor]Parameter types to lib.d.ts (#26243) 2018-08-31 07:46:16 -07:00
Nathan Shively-Sanders cc3d011333
Infer this parameters (#26800)
Previously we didn't. I can't remember why, probably because I
overlooked it in the initial PR.
2018-08-31 07:45:34 -07:00
Nathan Shively-Sanders b687caf3eb
No excess property error for spread properties (#26798)
That is, properties in an object literal type that came from a spread
assignment never cause an excess property error.
2018-08-30 16:16:58 -07:00
Nathan Shively-Sanders d3f96015f1
Fix namespace expando merge (#26690)
* Allow JSContainers to merge with namespaces

Expando functions marked with JSContainer previously failed to merge
with namespaces. This change adds JSContainer to ValueModuleExcludes,
allowing this kind of merge.

* Improve symbol flags to fix namespace/expando merging

Calls to bindPropertyAssignment now provide which special assignment
kind they originated from. This allows better symbol flags to be set:

1. Property assignments get the FunctionScopedVariable flag, since they are
equivalent to a `namespace` exporting a `var`.
2. Prototype property assignments get the Method flag if the initialiser
is functionlike, and Property otherwise.
3. Prototype assignments get the flag Property.

(3) is still not entirely correct (it's missing the Prototype flag),
but is what existed previously. I'll try adding the Prototype flag to
see whether it changes any baselines.

* Add cross-file merge test

* Update missed baselines

* Namespace declarations are primary for merging purposes

Also, property-assignments go back to being property declarations, not
function-scoped variable declarations

* Revert unneeded changes

* Revert unneeded changes (in a codefix this time)

* Put JSContainer on all assignment declarations

This allows most of the new special-case merge code to go away. It now
uses the JSContainer special-case code, which already exists.

* Missed comment

* Fix extra newline lint
2018-08-30 13:18:50 -07:00
Tim Schaub 20a2b0cade Ignore newline and asterisk when parsing JSDoc typedef (#26775) 2018-08-30 10:01:33 -07:00
Wenlu Wang 038f665171 fix lookup regression again (#26762)
* fix lookup regression again

* add test case
2018-08-30 08:48:49 -07:00
Anders Hejlsberg a28791565d
Merge pull request #26746 from Microsoft/noUnionInferences
Don't infer unions for disjoint callback parameter candidates
2018-08-29 15:45:43 -07:00
Nathan Shively-Sanders 29dbabe2e1
In JS, fix contextual type of this assignments (#26743)
in object literal methods inside an object literal with a type
annotation.

Note that this does not change:

1. The type of `this` in object literal methods.
2. The fact that this-property assignments are still declarations. They
just don't block contextual typing like most declarations do.

This change is a bit expensive. It first calls getThisContainer, which
walks the tree upward. Then it calls checkThisExpression, which will
usually call getContextualType on the object literal method. If the new
code then returns true, it will proceed to redo much of that work.

Calling checkThisExpression should not cause incorrect circularity
failures; we only have to inspect the shape of the object literal and
not the types of its properties to determine its type.
2018-08-29 15:06:38 -07:00
Anders Hejlsberg c48c3632bd Update tests 2018-08-29 14:02:15 -07:00
Ron Buckton 210de32933 Merge branch 'master' into typesVersions 2018-08-29 13:26:13 -07:00
Anders Hejlsberg a2d2f5aee6 Merge branch 'master' into complexRestParameterTypes 2018-08-29 07:51:07 -07:00
Wenlu Wang f67d7e01cf add test case and fix regression (#26726) 2018-08-29 06:58:55 -07:00
王文璐 597bb0e764 Merge branch 'master' into strictParameter 2018-08-29 18:28:36 +08:00
Ron Buckton 5f6a2cbf69 Merge branch 'master' into typesVersions 2018-08-28 17:37:23 -07:00
Ron Buckton 37c33f4369 Use semver ranges 2018-08-28 17:24:02 -07:00
Ron Buckton 37ec065d93 Add path back-reference tests 2018-08-28 11:44:40 -07:00
Ron Buckton dc5cd9676b Switch to paths-like pattern matching 2018-08-28 09:33:03 -07:00
王文璐 f1c5fa5b35 Merge branch 'master' into strictParameter 2018-08-28 17:37:58 +08:00
王文璐 8869f39c25 accept more case 2018-08-28 16:41:26 +08:00
王文璐 d758075597 add special check for parameter initializer lookup if targeting es2015+ 2018-08-28 15:02:28 +08:00
Anders Hejlsberg bd40583beb
Merge pull request #26698 from Microsoft/indexedAccessConstraints
Improve indexed access type relations
2018-08-27 18:14:43 -07:00
Nathan Shively-Sanders a2e4a282e7
Get [type] parameter types from @type tag (#26694)
* Get [type] parameter types from @type tag

Previously only the return type was used in cases like this:

```js
/** @type {<T>(param?: T) => T | undefined} */
function g(param) {
  return param;
}
```

Now the type parameters from the type tag are used, and the compiler
gets the type of the parameter by using the position in the signature of
the type tag.

Fixes #25618

* Fix split ifs according to PR comments
2018-08-27 16:52:35 -07:00
Anders Hejlsberg 9f83958dbe Add tests 2018-08-27 16:06:17 -07:00
Nathan Shively-Sanders b50c37de78
No assert for nameless typedefs (#26695)
The assert is over-optimistic and should be removed until we can parse
every possible thing that people might put in a JSDoc type position.

Fixes #26693
2018-08-27 14:12:14 -07:00
Nathan Shively-Sanders 6419240ab2
Declaration emit includes function properties (#26499)
* Declaration emit includes function properties

It does this by printing the type as an object literal type:

```ts
function f() { }
f.p = 1
```

Appears in a d.ts as

```ts
declare var f: {
  (): void;
  p: number;
}
```

It would also be possible to represent it as a namespace merge. I'm not
sure which is better.

```ts
declare function f(): void;
declare namespace f {
  export var p: number;
}
```

In order to avoid a private-name-used error (though I think it was
actually *unused*), I also had to change the nodeBuilder code to match.
This is arguably harder to read. So it's possible that I should instead
keep the nodeBuilder version as `typeof f` and make an exception for
private name use.

* Emit namespace merge instead of object type

This makes the change smaller, overall.

* Fix isJSContainerFunctionDeclaration+namespace merges

Also improve emit style to match other namespace emit.

* Add isPrivate + test case from PR comments
2018-08-27 10:29:53 -07:00
Nathan Day 3ec2c45f5f include leading non-ASCII horizontal whitespace in SyntaxKind.WhitespaceTrivia token 2018-08-25 13:15:56 -04:00
Anders Hejlsberg 676892ee56 Add tests 2018-08-25 07:55:13 -07:00
Nathan Shively-Sanders 0043ba16b1
Allow weak type detection for intersection sources (#26668)
Previously, intersections were only allowed as targets, but this was
just an artifact of the original implementation, which operated inside
the structural part of isRelatedTo. Removing this restriction catches
subtle bugs in React user code, where a function named `create` returns
a mapped type whose types are all branded numbers. The display of these
properties, for some original type `T`, is not `number & { __ }` but
the much-less-obvious `RegisteredStyle<T>`.
2018-08-24 10:30:39 -07:00
Nathan Shively-Sanders 03653934c3
Don't create expando object literals in TS (#26525)
Previously, getWidenedTypedFromJSPropertyAssignment was not called for
Typescript code. Since property assignments on functions, it is. That
meant that property assignments would incorrectly create a JS container
for empty object literals in a property assignment, even in Typescript:

```ts
const one = () => 1
one.p = {}
one.p.q = {} // should not work in Typescript!
```

Now empty object literals never create expando objects in Typescript,
because getJSExpandoObjectType requires the declaration to be in a JS
file.
2018-08-23 08:21:28 -07:00
Anders Hejlsberg 74c57caa90 Add regression test 2018-08-22 07:16:31 -07:00
Ron Buckton 79d7f371bb Merge branch 'master' into typesVersions 2018-08-21 15:47:10 -07:00
Andy 72886512a6
When --noImplicitAny is enabled, don't report errors suggesting that a 'void' function can be 'new'ed (#26579) 2018-08-21 10:02:02 -07:00
Ron Buckton 42c9208fd1
Merge pull request #26564 from Microsoft/fix26497
Emit lib reference directives in declaration output
2018-08-21 09:48:07 -07:00
Ron Buckton 015babb6f7 Initial support for 'typesVersions' 2018-08-20 16:57:18 -07:00
Ron Buckton 1de8cd3f62 Emit lib reference directives in declaration output 2018-08-20 16:54:51 -07:00
Matt McCutchen cc1c2ab6b2 Go back to the old narrowing algorithm (pre #26143) and avoid #26130 by
skipping narrowing if the old algorithm produces a type to which the
assigned type is not assignable.

This also means we'll no longer narrow for erroneous assignments where
the assigned type is not assignable to the declared type.  This is the
reason for the numericLiteralTypes3 baseline change.

Fixes #26405.
2018-08-17 21:35:03 -04:00
Anders Hejlsberg 3e201e7809
Merge pull request #26517 from Microsoft/fixMappedArrayTypeConstraint
Fix mapped array type constraint
2018-08-17 10:52:42 -07:00
Anders Hejlsberg 596493cce4 Add tests 2018-08-17 10:11:28 -07:00
Tim Schaub 6fd725f3ea Skip whitespace or asterisk in JSDoc param type and name (#26067) 2018-08-16 16:16:09 -07:00
Nathan Shively-Sanders 75071a2509
Allow super references to constructor function methods (#26482)
Previously, they were mistakenly treated as private because of a check
that required all super property accesses (like `super.x()`) to be
references to a MethodDeclaration or MethodSignature. This change also
allows PrototypeProperty special assignment kinds.
2018-08-16 09:20:30 -07:00
Nathan Shively-Sanders cc67ce1141
Property assignments in Typescript (#26368)
* Allow special property assignments in TS

But only for functions and constant variable declarations initialised with
functions.

This specifically excludes class declarations and class expressions,
which differs from Javascript. That's because Typescript supports
`static` properties, which are equivalent to property assignments to a
class.

* Improve contextual typing predicate

Don't think it's right yet, but probably closer?

* More fixes.

The code is still fantastically ugly, but everything works the way it
should.

Also update baselines, even where it is ill-advised.

* Cleanup

* Remove extra whitespace

* Some kind of fix to isAnyDeclarationName

It's not done yet.

Specifically, in TS:
Special property assignments are supposed to be declaration sites (but not all
top-level assignments), and I think I
got them to be. (But not sure).

In JS:
Special property assignments are supposed to be declaration sites (but not all
top-level assignments), and I'm pretty sure ALL top-level assignments
have been declaration sites for some time. This is incorrect, and
probably means the predicate needs to be the same for both dialects.

* Add fourslash and improve isAnyDeclarationName

Now JS behaves the same as TS.

* Cleanup from PR comments
2018-08-15 15:25:25 -07:00
Nathan Shively-Sanders 08eb99d8ec
For a this-property assignment with an empty object initializer, use type annotation if present (#26428)
* This-property w/empty object init: use type annotation

* JS initializer type doesn't apply to this-property assignments

* Move getJSExpandoType into getWidenedType* functions

Plus some cleanup.

* Improved style from PR comments

* Parameters are not expando
2018-08-15 14:53:30 -07:00
Nathan Shively-Sanders 29ca93ba48
Classes can extend Javascript constructor functions (#26452)
* Classes can extend JS constructor functions

Now ES6 classes can extend ES5 constructor functions, although only
those written in a JS file.

Note that the static side assignability is checked. I need to write
tests to make sure that instance side assignability is checked too.
I haven't tested generic constructor functions yet either.

* Test static+instance assignability errors+generics

Note that generics do not work.

* Cleanup from PR comments

* Even more cleanup

* Update case of function name
2018-08-14 14:43:04 -07:00
James Keane a1089893bd Fixes #26128 - signature comp for jsdoc @class. (#26160)
* Fixes #26128 - signature comp for  jsdoc @class.

Another issue caused by js functions tagged with jsdoc
`@constructor` not having construct signatures.

A jsdoc function type that constructs a type (`function(new: Ex)`),
has a construct signature and return value inferred as the
constructed type where as a jsdoc `@constructor` has no construct
signatures, and it's call signature has a void return type
(or undefined).

i.e:
```javascript
/** @constructor **/ function E() {};

// typeof E -> call signature: () => void

/** @param {function(new: E)} d */ function c(d) {}

// typeof d -> construct: () => E
```

--

This commit fixes this (in an inelegant way) by considering `@class` function signatures as construct signatures and synthesizing it's return value _only for signature comparison_.

There might be a slight performance hit, since the synthesized return value is not cached; but changing the `@class` function's return type in `getReturnTypeOfSignature` causes other issues.

* Update jsdoc function test to fix mistake.
2018-08-14 13:35:51 -07:00
Nathan Shively-Sanders a6c5d50749
Allow type predicates in JSDoc (#26343)
* Allow type predicates

1. Parse type predicates. Note that they are parsed everywhere, and get
the appropriate error when used places besides a return type.
2. When creating a type predicate, correctly find the function's parameters
starting from the jsdoc return type.

* Fix type of TypePredicateNode.parent: add JSDocTypeExpression

* Update API baselines

* Handle JSDoc signature inside @type annotations

* Fix circularity when getting type predicates

Also move createTypePredicateFromTypePredicateNode closer to its use

* More cleanup based on review comments
2018-08-10 15:31:39 -07:00
王文璐 6432bd9def check index access for fixed length tuple 2018-08-10 10:49:37 +08:00
Andy 639fdcc916
Don't include class getter in spread type (#26287)
* Don't include class getter in spread type

* Code review
2018-08-09 15:34:29 -07:00
Anders Hejlsberg 01f6093a9c
Merge pull request #26143 from mattmccutchen/issue-26130
Have getAssignmentReducedType use the comparable relation instead of typeMaybeAssignableTo.
2018-08-09 07:43:46 -07:00
Anders Hejlsberg 6a17f4d162 Merge branch 'master' into fixCircularReturnType
# Conflicts:
#	tests/baselines/reference/recursiveResolveDeclaredMembers.types
#	tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.types
2018-08-06 10:42:35 -07:00
Anders Hejlsberg efdbbd1cf3 Add regression tests 2018-08-06 10:02:33 -07:00
Ryan Cavanaugh 3ab7a98ecf
Merge pull request #26121 from mattmccutchen/issue-23999
"Could not find a declaration file for module" error needs to use the unmangled package name where appropriate.
2018-08-01 20:55:31 -07:00
Matt McCutchen d45e422b46 Have getAssignmentReducedType use the comparable relation instead of
typeMaybeAssignableTo.

typeMaybeAssignableTo decomposed unions at the top level of the assigned
type but didn't properly handle other unions that arose during
assignability checking, e.g., in the constraint of a generic lookup
type.

Fixes #26130.
2018-08-01 23:26:17 -04:00
James Keane 50f442f9ff Fixes #26122 - erroneous "TS2350" for js constructors called with incorrect parameters (#26124)
* Fixes #26122.

When `resolveCall` does not resolve in `resolveNewExpression`, the error should only be thrown if there is a *defined* signature that is not-void.

* Fix other baselines to remove erroneous TS2350.
2018-08-01 13:40:55 -07:00
Matt McCutchen d054621477 "Could not find a declaration file for module" error needs to use the
unmangled package name where appropriate.

Add a test case for an untyped sub-module of a scoped package with
typings.  The other diagnostic message is covered by existing tests; I
guess no one looked at the baselines closely enough.

Fixes #23999.
2018-08-01 14:13:38 -04:00
Klaus Meinhardt 8630396d8a update test 2018-08-01 09:59:50 +02:00
王文璐 02f5365e08 improve error message and update testcase 2018-08-01 13:45:57 +08:00
王文璐 813f28a4f8 Merge branch 'master' into strictParameter 2018-08-01 10:11:13 +08:00
James Keane dfedb24f75 Jsdoc @constructor - in constructor properly infer this as class instance (#25980)
* Properly infer `this` in tagged `@constructor`s.

`c.prototype.method = function() { this }` was already supported.

This commit add support to two more kinds relying on the JSDoc
`@constructor` tag. These are:
 1. `/** @constructor */ function Example() { this }`
 2. `/** @constructor */ var Example = function() { this }`

* Update the baseline for js constructorFunctions.

C3 and C4 `this` was set as `any`, now it is properly showing as
the class type.

* Fix lint errors

* Add circular initialisers to constructo fn tests.

* Error (`TS2348`) if calling tagged js constructors

When calling a JS function explicitly tagged with either `@class` or
`@constructor` the checker should throw a TS2348 not callable error.

* Don't resolve jsdoc classes with construct sigs.

This undoes the last commit that sought to change how js functions
tagged with `@class` were inferred. For some reason, currently
unknown, giving those functions construct signatures causes issues
in property assignment/member resolution (as seen in the
`typeFromPropertyAssignment12` test case).

Instead of changing the signature resolution, the error is explicitly
generated in `resolveCallExpression` for those functions.
2018-07-31 13:52:39 -07:00
Nathan Shively-Sanders a21ac11582
In JSDoc, resolve import types as values too (#26066)
* In JSDoc, resolve import types as values too

This is something that we probably should have been doing for some time.
Fixes #26049

* Fix whitespace lint
2018-07-31 11:07:06 -07:00
Anders Hejlsberg 4bc7f1570b
Merge pull request #26063 from Microsoft/mappedTypesArraysTuples
Improved mapped type support for arrays and tuples
2018-07-31 10:54:44 -07:00
Nathan Shively-Sanders 4d84bde9b3
Only bind module.exports if no local definition exists (#25869)
* Only bind module.exports if no local definition exists

Note that this uses `lookupSymbolForNameWorker`, which is really a
best-effort check since it only knows about symbols that it has already
encountered.

As a side-effect, even when `module` is bound as part of a
`module.exports` reference, it only declares it once instead of one
declaration per reference.

* Only type module.exports inside module files

It is an error inside script files, but the binder sometimes creates a
ModuleExports symbol because we doesn't know whether we have a commonjs
module until after binding is done.

* Only bind module.exports in a commonjs module

Note that this, too, is a best-effort check since evidence of
commonjs-ness may be found after a *reference* to module.exports. (A
reference to module.exports alone is not enough evidence that a file is
commonjs. It has to have an assignment to it.)
2018-07-30 12:27:59 -07:00
Anders Hejlsberg 32a9ec6c30 Add tests 2018-07-29 15:25:54 -07:00
Nathan Shively-Sanders 25fb5419c0
Support the JSDoc @enum tag (#26021)
* Support the JSDoc @enum tag

`@enum` is used on a variable declaration with an object literal
initializer. It does a number of things:

1. The object literal has a closed set of properties, unlike other
object literals in Javascript.
2. The variable's name is resolvable as a type, but it just has the
declared type of the enum tag.
3. Each property's type must be assignable to the enum tag's declared type,
which can be any type.

For example,

```js
/** @enum {string} */
const Target = {
  START: "START",
  END: "END",
  MISTAKE: 0, // error 'number' is not assignable to 'string' -- see (3)
}

Target.THIS_IS_AN_ERROR; // See (1)
/** @type {Target} See (2) */
var target = Target.START;
```

* Fix lint, add new test case, update API baselines
2018-07-28 07:53:08 -07:00
王文璐 c531065fd0 Merge branch 'master' into definite-assignment-assertion-improve 2018-07-26 18:06:47 +08:00
Anders Hejlsberg 1aa2b15f8c Add regression test 2018-07-25 12:42:47 -07:00
Anders Hejlsberg dd4fd8c60e
Merge pull request #25913 from Microsoft/fixCircularConstraintCheck
Fix circular constraint check
2018-07-25 06:46:51 -07:00
Anders Hejlsberg aeae05eaf5 Add regression test 2018-07-24 15:33:02 -07:00
Matt McCutchen f72193eedc Report a semantic error for an arrow function with a "this" parameter.
Fixes #9744.
2018-07-23 10:42:56 -04:00
Nathan Shively-Sanders 31d599abaf
Check module.exports (#25732)
* Revert "Revert "Explicitly typed special assignments are context sensitive (#25619)""

This reverts commit 16676f2707.

* Revert "Revert "Explicitly typed prototype assignments are context sensitive (#25688)""

This reverts commit ff8c30d636.

* Initial, wasteful, solution

It burns a check flags. Probably necessary, but perhaps not.

I haven't accepted baselines, but they are a bit questionable. I'm not
sure the synthetic type is right, because I expected to see
{ "exports": typeof import("x") } but instead see { "x": typeof
import("x") }.

* Update some baselines

* module.exports= always uses lhs type

Conflicts between exports property assignments and exports assignments
should get a union type instead of an error.

* Fix lint and accept good user baselines

* Add tests based on user tests.

These currently fail.

* Fix all but 1 of user test bugs found by typing module.exports

Still quite messy and full of notes

* Follow merged symbols+allow any object type

This allows exports like `module.exports = new EE` to have properties
added to them.

Still messy, but I'm going to run user tests and look for regressions.

* Update improved user baselines

* Fix infinite recursion when checking module.exports

* Fix bogus use-before-def error

getExportSymbolOfValueSymbolIfExported should always merge its returned
symbol, whether it's symbol.exportSymbol or just symbol.

* Update user test baselines

* Cleanup

* More small cleanup

* Bind `module` of `module.exports` as a special symbol

Previously it was also special, but created during name resolution in
the checker. It made sense when there was only one special symbol for
all files, but now there is one `module` symbol per file.
2018-07-20 10:59:26 -07:00
Nathan Shively-Sanders 1cedab18be
Fix parsing of parenthesized JSDoc parameters (#25799)
* Fix parsing of parenthesized JSDoc parameters

Parenthesis can start a jsdoc function parameter since it is just a
type, and parenthesis can start a type:

```js
/** @type {function(((string))): void} */
```

However, this is not legal in other parameter lists:

```ts
function x((((a))): string) { }
```

This change makes jsdoc function parameter lists parse differently than
normal parameter lists by allowing parenthesis as a start character of
jsdoc parameters.

* Parse nested uses of jsdoc function types

* Fix test
2018-07-19 12:50:36 -07:00
Wenlu Wang ed8b76424e add grammar check for labeled declaration (#25317)
* add grammar check for labeled function declaration

* fix debug failed on labeled class declaration

* move labeled statement check to binder and add more pattern for check

* update diagnostic message

* update baseline
2018-07-18 15:37:27 -06:00
Nathan Shively-Sanders 1edc975f15
Revert the revert of explicitly typed special assignments (#25727)
* Revert "Revert "Explicitly typed special assignments are context sensitive (#25619)""

This reverts commit 16676f2707.

* Revert "Revert "Explicitly typed prototype assignments are context sensitive (#25688)""

This reverts commit ff8c30d636.
2018-07-17 10:02:51 -07:00
Nathan Shively-Sanders 16676f2707 Revert "Explicitly typed special assignments are context sensitive (#25619)"
This reverts commit 32e60a9647.
2018-07-16 12:39:08 -07:00
Nathan Shively-Sanders ff8c30d636 Revert "Explicitly typed prototype assignments are context sensitive (#25688)"
This reverts commit 5b21cbc0c9.
2018-07-16 12:38:27 -07:00
Nathan Shively-Sanders 5b21cbc0c9
Explicitly typed prototype assignments are context sensitive (#25688)
* Explicitly typed prototype assignments:ctx sensitive

Follow up to #25619: Add the necessary code to type `prototype`
correctly in prototype assignments so that code like
`F.prototype = { ... }` properly makes the object literal context
sensitive.

* Fix lint
2018-07-16 10:03:39 -07:00
王文璐 cdfef4fa57 add use strict and simple parameter check 2018-07-16 17:40:57 +08:00
Nathan Shively-Sanders 32e60a9647
Explicitly typed special assignments are context sensitive (#25619)
* Explicitly typed js assignments: context sensitive

Explicitly typed special assignments should be context sensitive if they
have an explicit type tag. Previously no special assignments were
context sensitive because they are declarations, and in the common,
untyped, case we inspect the right side of the assignment to get the
type of the left side, and inspect the right side of the assignment to
get the type of the left side, etc etc.

Note that some special assignments still return `any` from
checkExpression, so still don't get the right type.

Fixes #25571

* Change prototype property handling+update bselines

* Fix indentation in test

* Update baselines
2018-07-12 15:28:53 -07:00
Andy f500289a44
Stricter test that JSDoc @type tag matches function signature (#25615) 2018-07-12 14:02:02 -07:00
Anders Hejlsberg 5822a8c923 Merge branch 'master' into genericRestArityCheck
# Conflicts:
#	src/compiler/checker.ts
2018-07-12 08:20:48 -10:00
Nathan Shively-Sanders bd7b97ce61
Get return type from @type tag (#25580)
* Get return type from `@type` tag

This only happens in the checker, where the type is easily accessible.
The syntax-based check in getEffectiveReturnTypeNode as a fast path, and
for other uses that don't want to make a call to getTypeFromTypeNode.

Fixes #25525

* Implement PR suggestions

* Error when type tag isn't callable

* Fix lint
2018-07-12 10:49:41 -07:00
Anders Hejlsberg 55180f7725 Add tests 2018-07-12 07:07:13 -10:00
Nathan Shively-Sanders 42a2d9e568
Excess property understands conditional types (#25584)
Previously it did not, causing misleading excess property errors. Note
that assignability errors with conditional types are still usually
confusing. This PR doesn't address that.

Also, make sure that exact matches in getSpellingSuggestion are skipped.
2018-07-11 11:24:40 -07:00
Nathan Shively-Sanders c344a3ea5b
Fix bogus use before def in jsdoc (#25532)
Block scoped variables, classes and enums would issue a bogus
use-before-def error in jsdoc because name resolution always adds Value,
even when resolving a type.

Fixes #25097
2018-07-10 08:33:19 -07:00