- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.1k
Strict function types #18654
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
          
     Merged
      
      
    
  
     Merged
                    Strict function types #18654
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            25 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      12f5dd8
              
                Introduce --strictFunctionTypes mode
              
              
                ahejlsberg f8ff7f7
              
                Use dedicated marker types for variance determination
              
              
                ahejlsberg 670d711
              
                Add quick path for computing array variance as it is already known
              
              
                ahejlsberg a0fa69f
              
                Handle contravariance in type inference
              
              
                ahejlsberg b58e0fb
              
                Add comments
              
              
                ahejlsberg 84f7afd
              
                Handle special case of 'void' type arguments for covariant type param…
              
              
                ahejlsberg 54eadef
              
                Accept new baselines
              
              
                ahejlsberg 44cc8c5
              
                Use methods in dom.generated.d.ts to opt out of strict checks
              
              
                ahejlsberg dd466ae
              
                Update tsconfig baselines
              
              
                ahejlsberg 24698dd
              
                Revert dom.generated.d.ts and fix duplicate declarations
              
              
                ahejlsberg f8e2cc1
              
                Properly flag and structurally compare marker type references
              
              
                ahejlsberg 589e1f4
              
                Update comment
              
              
                ahejlsberg afc8a26
              
                Always perform structural comparison when variance check fails
              
              
                ahejlsberg 70e8f73
              
                Add tests
              
              
                ahejlsberg 91691f6
              
                Strict function type checking only for certain function types
              
              
                ahejlsberg 6a481e8
              
                Update tests
              
              
                ahejlsberg 1795614
              
                Accept new baselines
              
              
                ahejlsberg 5613be4
              
                Only methods and constructors are bivariant in --strictFunctionTypes …
              
              
                ahejlsberg 1609196
              
                Accept new baselines
              
              
                ahejlsberg 0756aa1
              
                Merge branch 'master' into strictFunctionTypes
              
              
                ahejlsberg c626d9d
              
                Accept new baselines
              
              
                ahejlsberg 936f98d
              
                Addressing CR feedback
              
              
                ahejlsberg bf75a3f
              
                Emit .d.ts file in test
              
              
                ahejlsberg bff843a
              
                Improve error elaboration for invariant generic types
              
              
                ahejlsberg c2344e0
              
                Add error elaboration test
              
              
                ahejlsberg File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //// [strictFunctionTypes1.ts] | ||
| declare function f1<T>(f1: (x: T) => void, f2: (x: T) => void): (x: T) => void; | ||
| declare function f2<T>(obj: T, f1: (x: T) => void, f2: (x: T) => void): T; | ||
| declare function f3<T>(obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void): T; | ||
|  | ||
| interface Func<T> { (x: T): void } | ||
|  | ||
| declare function f4<T>(f1: Func<T>, f2: Func<T>): Func<T>; | ||
|  | ||
| declare function fo(x: Object): void; | ||
| declare function fs(x: string): void; | ||
| declare function fx(f: (x: "def") => void): void; | ||
|  | ||
| const x1 = f1(fo, fs); // (x: string) => void | ||
| const x2 = f2("abc", fo, fs); // "abc" | ||
| const x3 = f3("abc", fo, fx); // "abc" | "def" | ||
| const x4 = f4(fo, fs); // Func<string> | ||
|  | ||
|  | ||
| //// [strictFunctionTypes1.js] | ||
| "use strict"; | ||
| var x1 = f1(fo, fs); // (x: string) => void | ||
| var x2 = f2("abc", fo, fs); // "abc" | ||
| var x3 = f3("abc", fo, fx); // "abc" | "def" | ||
| var x4 = f4(fo, fs); // Func<string> | ||
|  | ||
|  | ||
| //// [strictFunctionTypes1.d.ts] | ||
| declare function f1<T>(f1: (x: T) => void, f2: (x: T) => void): (x: T) => void; | ||
| declare function f2<T>(obj: T, f1: (x: T) => void, f2: (x: T) => void): T; | ||
| declare function f3<T>(obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void): T; | ||
| interface Func<T> { | ||
| (x: T): void; | ||
| } | ||
| declare function f4<T>(f1: Func<T>, f2: Func<T>): Func<T>; | ||
| declare function fo(x: Object): void; | ||
| declare function fs(x: string): void; | ||
| declare function fx(f: (x: "def") => void): void; | ||
| declare const x1: (x: string) => void; | ||
| declare const x2 = "abc"; | ||
| declare const x3: string; | ||
| declare const x4: Func<string>; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| === tests/cases/compiler/strictFunctionTypes1.ts === | ||
| declare function f1<T>(f1: (x: T) => void, f2: (x: T) => void): (x: T) => void; | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 0)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 23)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 28)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 42)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 48)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 0, 65)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 0, 20)) | ||
|  | ||
| declare function f2<T>(obj: T, f1: (x: T) => void, f2: (x: T) => void): T; | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 79)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
| >obj : Symbol(obj, Decl(strictFunctionTypes1.ts, 1, 23)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 1, 30)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 1, 36)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 1, 50)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 1, 56)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 1, 20)) | ||
|  | ||
| declare function f3<T>(obj: T, f1: (x: T) => void, f2: (f: (x: T) => void) => void): T; | ||
| >f3 : Symbol(f3, Decl(strictFunctionTypes1.ts, 1, 74)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
| >obj : Symbol(obj, Decl(strictFunctionTypes1.ts, 2, 23)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 2, 30)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 2, 36)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 2, 50)) | ||
| >f : Symbol(f, Decl(strictFunctionTypes1.ts, 2, 56)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 2, 60)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 2, 20)) | ||
|  | ||
| interface Func<T> { (x: T): void } | ||
| >Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 4, 15)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 4, 21)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 4, 15)) | ||
|  | ||
| declare function f4<T>(f1: Func<T>, f2: Func<T>): Func<T>; | ||
| >f4 : Symbol(f4, Decl(strictFunctionTypes1.ts, 4, 34)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 6, 23)) | ||
| >Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 6, 35)) | ||
| >Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
| >Func : Symbol(Func, Decl(strictFunctionTypes1.ts, 2, 87)) | ||
| >T : Symbol(T, Decl(strictFunctionTypes1.ts, 6, 20)) | ||
|  | ||
| declare function fo(x: Object): void; | ||
| >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 8, 20)) | ||
| >Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) | ||
|  | ||
| declare function fs(x: string): void; | ||
| >fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 9, 20)) | ||
|  | ||
| declare function fx(f: (x: "def") => void): void; | ||
| >fx : Symbol(fx, Decl(strictFunctionTypes1.ts, 9, 37)) | ||
| >f : Symbol(f, Decl(strictFunctionTypes1.ts, 10, 20)) | ||
| >x : Symbol(x, Decl(strictFunctionTypes1.ts, 10, 24)) | ||
|  | ||
| const x1 = f1(fo, fs); // (x: string) => void | ||
| >x1 : Symbol(x1, Decl(strictFunctionTypes1.ts, 12, 5)) | ||
| >f1 : Symbol(f1, Decl(strictFunctionTypes1.ts, 0, 0)) | ||
| >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
| >fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|  | ||
| const x2 = f2("abc", fo, fs); // "abc" | ||
| >x2 : Symbol(x2, Decl(strictFunctionTypes1.ts, 13, 5)) | ||
| >f2 : Symbol(f2, Decl(strictFunctionTypes1.ts, 0, 79)) | ||
| >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
| >fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|  | ||
| const x3 = f3("abc", fo, fx); // "abc" | "def" | ||
| >x3 : Symbol(x3, Decl(strictFunctionTypes1.ts, 14, 5)) | ||
| >f3 : Symbol(f3, Decl(strictFunctionTypes1.ts, 1, 74)) | ||
| >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
| >fx : Symbol(fx, Decl(strictFunctionTypes1.ts, 9, 37)) | ||
|  | ||
| const x4 = f4(fo, fs); // Func<string> | ||
| >x4 : Symbol(x4, Decl(strictFunctionTypes1.ts, 15, 5)) | ||
| >f4 : Symbol(f4, Decl(strictFunctionTypes1.ts, 4, 34)) | ||
| >fo : Symbol(fo, Decl(strictFunctionTypes1.ts, 6, 58)) | ||
| >fs : Symbol(fs, Decl(strictFunctionTypes1.ts, 8, 37)) | ||
|  | 
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should probably be
@internalThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably, but not really related to this PR since the type already existed before. I'm going to leave it be for now.