diff --git a/tsc/internal/checker/pseudotypenodebuilder.go b/tsc/internal/checker/pseudotypenodebuilder.go index 8f1e2bf1700e1..51f2ba6370999 100644 --- a/tsc/internal/checker/pseudotypenodebuilder.go +++ b/tsc/internal/checker/pseudotypenodebuilder.go @@ -430,6 +430,7 @@ func (b *NodeBuilderImpl) pseudoTypeEquivalentToType(t *pseudochecker.PseudoType if len(pt.Elements) != targetDeclCount { return false } + isConst := len(pt.Elements) > 0 && b.ch.isConstContext(pt.Elements[0].Name.Parent.Parent) for _, e := range pt.Elements { var targetProp *ast.Symbol elemSymbol := e.Name.Parent.Symbol() @@ -452,6 +453,17 @@ func (b *NodeBuilderImpl) pseudoTypeEquivalentToType(t *pseudochecker.PseudoType return false } } + if e.Kind == pseudochecker.PseudoObjectElementKindPropertyAssignment || e.Kind == pseudochecker.PseudoObjectElementKindMethod { + // Const inference can mark symbols readonly even when the resolved contextual type is no longer const. + // Only reuse syntax if it will preserve the checker's readonly state. + isReadonly := isConst || e.Kind == pseudochecker.PseudoObjectElementKindPropertyAssignment && e.AsPseudoPropertyAssignment().Readonly + if isReadonly != b.ch.isReadonlySymbol(targetProp) { + if reportErrors { + b.ctx.tracker.ReportInferenceFallback(e.Name.Parent) + } + return false + } + } targetIsOptional := targetProp.Flags()&ast.SymbolFlagsOptional != 0 if e.Optional != targetIsOptional { if reportErrors { diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).js b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).js new file mode 100644 index 0000000000000..bf63e95bc0ef3 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).js @@ -0,0 +1,111 @@ +//// [tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts] //// + +//// [declarationEmitConstTypeParameterReadonly.ts] +declare function id(c: C): C; +export const b = id({ A: { f: () => 1 } }); +export const annotatedFunction = id({ A: { f: (): number => 1 } }); + +const leaf = { a: 1 } as const; +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); + +export const nested = id({ A: [{ f: () => 1 }] }); + +const tupleLeaf = [1, "a"] as const; +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); + +export const getterOnly = { + get value(): number { + return 1; + }, +}; + +export const getterAndSetter = { + get value(): number { + return 1; + }, + set value(value: number) {}, +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); + +declare function addReadonly(arg: { value: T }): { + value: { readonly [K in keyof T]: T[K] }; +}; +declare function removeReadonly(arg: { value: T }): { + value: { -readonly [K in keyof T]: T[K] }; +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); + + + + +//// [declarationEmitConstTypeParameterReadonly.d.ts] +export declare const b: { + readonly A: { + readonly f: () => 1; + }; +}; +export declare const annotatedFunction: { + readonly A: { + readonly f: () => number; + }; +}; +export declare const twoRefs: { + readonly properties: { + readonly one: { + readonly a: 1; + }; + readonly two: { + readonly a: 1; + }; + }; +}; +export declare const nested: { + readonly A: readonly [{ + readonly f: () => 1; + }]; +}; +export declare const twoTupleRefs: { + readonly properties: { + readonly one: readonly [1, "a"]; + readonly two: readonly [1, "a"]; + }; +}; +export declare const getterOnly: { + readonly value: number; +}; +export declare const getterAndSetter: { + get value(): number; + set value(value: number); +}; +export declare const mappedScalars: { + foo: "str"; + bar: 42; +}; +export declare const readonlyParameter: { + value: { + readonly f: () => 1; + }; +}; +export declare const mutableParameter: { + readonly value: { + readonly f: () => 1; + }; +}; +export declare const readonlyResult: { + value: { + readonly f: () => 1; + }; +}; +export declare const mutableResult: { + value: { + f: () => 1; + }; +}; diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).symbols new file mode 100644 index 0000000000000..fe8b4b04bc819 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).symbols @@ -0,0 +1,161 @@ +//// [tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts] //// + +=== declarationEmitConstTypeParameterReadonly.ts === +declare function id(c: C): C; +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) +>c : Symbol(c, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 29)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) + +export const b = id({ A: { f: () => 1 } }); +>b : Symbol(b, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 21)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 26)) + +export const annotatedFunction = id({ A: { f: (): number => 1 } }); +>annotatedFunction : Symbol(annotatedFunction, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 37)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 42)) + +const leaf = { a: 1 } as const; +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) +>a : Symbol(a, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 14)) +>const : Symbol(const) + +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); +>twoRefs : Symbol(twoRefs, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>properties : Symbol(properties, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 27)) +>one : Symbol(one, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 41)) +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) +>two : Symbol(two, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 52)) +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) + +export const nested = id({ A: [{ f: () => 1 }] }); +>nested : Symbol(nested, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 26)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 32)) + +const tupleLeaf = [1, "a"] as const; +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) +>const : Symbol(const) + +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); +>twoTupleRefs : Symbol(twoTupleRefs, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>properties : Symbol(properties, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 32)) +>one : Symbol(one, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 46)) +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) +>two : Symbol(two, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 62)) +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) + +export const getterOnly = { +>getterOnly : Symbol(getterOnly, Decl(declarationEmitConstTypeParameterReadonly.ts, 12, 12)) + + get value(): number { +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 12, 27)) + + return 1; + }, +}; + +export const getterAndSetter = { +>getterAndSetter : Symbol(getterAndSetter, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 12)) + + get value(): number { +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 32), Decl(declarationEmitConstTypeParameterReadonly.ts, 21, 6)) + + return 1; + }, + set value(value: number) {}, +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 32), Decl(declarationEmitConstTypeParameterReadonly.ts, 21, 6)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 22, 14)) + +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 40)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) + +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; +>mutableInput : Symbol(mutableInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 83)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 39)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +>mappedScalars : Symbol(mappedScalars, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 12)) +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>foo : Symbol(foo, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 44)) +>bar : Symbol(bar, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 56)) + +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +>readonlyParameter : Symbol(readonlyParameter, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 12)) +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 48)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 57)) + +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); +>mutableParameter : Symbol(mutableParameter, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 12)) +>mutableInput : Symbol(mutableInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 83)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 46)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 55)) + +declare function addReadonly(arg: { value: T }): { +>addReadonly : Symbol(addReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 72)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 38)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 44)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) + + value: { readonly [K in keyof T]: T[K] }; +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 59)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 33, 23)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 33, 23)) + +}; +declare function removeReadonly(arg: { value: T }): { +>removeReadonly : Symbol(removeReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 34, 2)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 41)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 47)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) + + value: { -readonly [K in keyof T]: T[K] }; +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 62)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 36, 24)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 36, 24)) + +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +>readonlyResult : Symbol(readonlyResult, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 12)) +>addReadonly : Symbol(addReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 72)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 43)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 52)) + +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); +>mutableResult : Symbol(mutableResult, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 12)) +>removeReadonly : Symbol(removeReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 34, 2)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 45)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 54)) +>const : Symbol(const) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).types b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).types new file mode 100644 index 0000000000000..ababa55394013 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=false).types @@ -0,0 +1,191 @@ +//// [tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts] //// + +=== declarationEmitConstTypeParameterReadonly.ts === +declare function id(c: C): C; +>id : (c: C) => C +>c : C + +export const b = id({ A: { f: () => 1 } }); +>b : { readonly A: { readonly f: () => 1; }; } +>id({ A: { f: () => 1 } }) : { readonly A: { readonly f: () => 1; }; } +>id : (c: C) => C +>{ A: { f: () => 1 } } : { A: { f: () => 1; }; } +>A : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const annotatedFunction = id({ A: { f: (): number => 1 } }); +>annotatedFunction : { readonly A: { readonly f: () => number; }; } +>id({ A: { f: (): number => 1 } }) : { readonly A: { readonly f: () => number; }; } +>id : (c: C) => C +>{ A: { f: (): number => 1 } } : { A: { f: () => number; }; } +>A : { f: () => number; } +>{ f: (): number => 1 } : { f: () => number; } +>f : () => number +>(): number => 1 : () => number +>1 : 1 + +const leaf = { a: 1 } as const; +>leaf : { readonly a: 1; } +>{ a: 1 } as const : { readonly a: 1; } +>{ a: 1 } : { readonly a: 1; } +>a : 1 +>1 : 1 + +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); +>twoRefs : { readonly properties: { readonly one: { readonly a: 1; }; readonly two: { readonly a: 1; }; }; } +>id({ properties: { one: leaf, two: leaf } }) : { readonly properties: { readonly one: { readonly a: 1; }; readonly two: { readonly a: 1; }; }; } +>id : (c: C) => C +>{ properties: { one: leaf, two: leaf } } : { properties: { one: { readonly a: 1; }; two: { readonly a: 1; }; }; } +>properties : { one: { readonly a: 1; }; two: { readonly a: 1; }; } +>{ one: leaf, two: leaf } : { one: { readonly a: 1; }; two: { readonly a: 1; }; } +>one : { readonly a: 1; } +>leaf : { readonly a: 1; } +>two : { readonly a: 1; } +>leaf : { readonly a: 1; } + +export const nested = id({ A: [{ f: () => 1 }] }); +>nested : { readonly A: readonly [{ readonly f: () => 1; }]; } +>id({ A: [{ f: () => 1 }] }) : { readonly A: readonly [{ readonly f: () => 1; }]; } +>id : (c: C) => C +>{ A: [{ f: () => 1 }] } : { A: [{ f: () => 1; }]; } +>A : [{ f: () => 1; }] +>[{ f: () => 1 }] : [{ f: () => 1; }] +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +const tupleLeaf = [1, "a"] as const; +>tupleLeaf : readonly [1, "a"] +>[1, "a"] as const : readonly [1, "a"] +>[1, "a"] : readonly [1, "a"] +>1 : 1 +>"a" : "a" + +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); +>twoTupleRefs : { readonly properties: { readonly one: readonly [1, "a"]; readonly two: readonly [1, "a"]; }; } +>id({ properties: { one: tupleLeaf, two: tupleLeaf } }) : { readonly properties: { readonly one: readonly [1, "a"]; readonly two: readonly [1, "a"]; }; } +>id : (c: C) => C +>{ properties: { one: tupleLeaf, two: tupleLeaf } } : { properties: { one: readonly [1, "a"]; two: readonly [1, "a"]; }; } +>properties : { one: readonly [1, "a"]; two: readonly [1, "a"]; } +>{ one: tupleLeaf, two: tupleLeaf } : { one: readonly [1, "a"]; two: readonly [1, "a"]; } +>one : readonly [1, "a"] +>tupleLeaf : readonly [1, "a"] +>two : readonly [1, "a"] +>tupleLeaf : readonly [1, "a"] + +export const getterOnly = { +>getterOnly : { readonly value: number; } +>{ get value(): number { return 1; },} : { readonly value: number; } + + get value(): number { +>value : number + + return 1; +>1 : 1 + + }, +}; + +export const getterAndSetter = { +>getterAndSetter : { value: number; } +>{ get value(): number { return 1; }, set value(value: number) {},} : { value: number; } + + get value(): number { +>value : number + + return 1; +>1 : 1 + + }, + set value(value: number) {}, +>value : number +>value : number + +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>arg : { readonly [K in keyof T]: T[K]; } + +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; +>mutableInput : (arg: { -readonly [K in keyof T]: T[K]; }) => T +>arg : { -readonly [K in keyof T]: T[K]; } + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +>mappedScalars : { foo: "str"; bar: 42; } +>readonlyInput({ foo: "str", bar: 42 }) : { foo: "str"; bar: 42; } +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>{ foo: "str", bar: 42 } : { foo: "str"; bar: 42; } +>foo : "str" +>"str" : "str" +>bar : 42 +>42 : 42 + +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +>readonlyParameter : { value: { readonly f: () => 1; }; } +>readonlyInput({ value: { f: () => 1 } }) : { value: { readonly f: () => 1; }; } +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); +>mutableParameter : { readonly value: { readonly f: () => 1; }; } +>mutableInput({ value: { f: () => 1 } }) : { readonly value: { readonly f: () => 1; }; } +>mutableInput : (arg: { -readonly [K in keyof T]: T[K]; }) => T +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +declare function addReadonly(arg: { value: T }): { +>addReadonly : (arg: { value: T; }) => { value: { readonly [K in keyof T]: T[K]; }; } +>arg : { value: T; } +>value : T + + value: { readonly [K in keyof T]: T[K] }; +>value : { readonly [K in keyof T]: T[K]; } + +}; +declare function removeReadonly(arg: { value: T }): { +>removeReadonly : (arg: { value: T; }) => { value: { -readonly [K in keyof T]: T[K]; }; } +>arg : { value: T; } +>value : T + + value: { -readonly [K in keyof T]: T[K] }; +>value : { -readonly [K in keyof T]: T[K]; } + +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +>readonlyResult : { value: { readonly f: () => 1; }; } +>addReadonly({ value: { f: () => 1 } }) : { value: { readonly f: () => 1; }; } +>addReadonly : (arg: { value: T; }) => { value: { readonly [K in keyof T]: T[K]; }; } +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); +>mutableResult : { value: { f: () => 1; }; } +>removeReadonly({ value: { f: () => 1 } } as const) : { value: { f: () => 1; }; } +>removeReadonly : (arg: { value: T; }) => { value: { -readonly [K in keyof T]: T[K]; }; } +>{ value: { f: () => 1 } } as const : { readonly value: { readonly f: () => 1; }; } +>{ value: { f: () => 1 } } : { readonly value: { readonly f: () => 1; }; } +>value : { readonly f: () => 1; } +>{ f: () => 1 } : { readonly f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).errors.txt b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).errors.txt new file mode 100644 index 0000000000000..7e2bcc0e00b21 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).errors.txt @@ -0,0 +1,159 @@ +declarationEmitConstTypeParameterReadonly.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(2,31): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(2,31): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(3,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(3,47): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(6,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(6,48): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(6,59): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(8,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(8,31): error TS9017: Only const arrays can be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(8,37): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(11,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(11,53): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(11,69): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(29,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(30,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(30,62): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(30,62): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(31,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(31,60): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(31,60): error TS9013: Expression type can't be inferred with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(40,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(40,57): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(41,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationEmitConstTypeParameterReadonly.ts(41,59): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. + + +==== declarationEmitConstTypeParameterReadonly.ts (25 errors) ==== + declare function id(c: C): C; + export const b = id({ A: { f: () => 1 } }); + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:2:14: Add a type annotation to the variable b. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:2:14: Add a type annotation to the variable b. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:2:31: Add a return type to the function expression. + ~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:2:14: Add a type annotation to the variable b. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:2:31: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + export const annotatedFunction = id({ A: { f: (): number => 1 } }); + ~~~~~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:3:14: Add a type annotation to the variable annotatedFunction. + ~~~~~~~~~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:3:14: Add a type annotation to the variable annotatedFunction. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:3:47: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + + const leaf = { a: 1 } as const; + export const twoRefs = id({ properties: { one: leaf, two: leaf } }); + ~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:6:14: Add a type annotation to the variable twoRefs. + ~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:6:14: Add a type annotation to the variable twoRefs. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:6:48: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + ~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:6:14: Add a type annotation to the variable twoRefs. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:6:59: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + + export const nested = id({ A: [{ f: () => 1 }] }); + ~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:8:14: Add a type annotation to the variable nested. + ~~~~~~~~~~~~~~~~ +!!! error TS9017: Only const arrays can be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:8:14: Add a type annotation to the variable nested. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:8:14: Add a type annotation to the variable nested. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:8:37: Add a return type to the function expression. + + const tupleLeaf = [1, "a"] as const; + export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); + ~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:11:14: Add a type annotation to the variable twoTupleRefs. + ~~~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:11:14: Add a type annotation to the variable twoTupleRefs. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:11:53: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + ~~~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:11:14: Add a type annotation to the variable twoTupleRefs. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:11:69: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + + export const getterOnly = { + get value(): number { + return 1; + }, + }; + + export const getterAndSetter = { + get value(): number { + return 1; + }, + set value(value: number) {}, + }; + + declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; + declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; + + export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); + ~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:29:14: Add a type annotation to the variable mappedScalars. + export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); + ~~~~~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:30:14: Add a type annotation to the variable readonlyParameter. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:30:14: Add a type annotation to the variable readonlyParameter. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:30:62: Add a return type to the function expression. + ~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:30:14: Add a type annotation to the variable readonlyParameter. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:30:62: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + export const mutableParameter = mutableInput({ value: { f: () => 1 } }); + ~~~~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:31:14: Add a type annotation to the variable mutableParameter. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:31:14: Add a type annotation to the variable mutableParameter. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:31:60: Add a return type to the function expression. + ~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:31:14: Add a type annotation to the variable mutableParameter. +!!! related TS9035 declarationEmitConstTypeParameterReadonly.ts:31:60: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + + declare function addReadonly(arg: { value: T }): { + value: { readonly [K in keyof T]: T[K] }; + }; + declare function removeReadonly(arg: { value: T }): { + value: { -readonly [K in keyof T]: T[K] }; + }; + + export const readonlyResult = addReadonly({ value: { f: () => 1 } }); + ~~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:40:14: Add a type annotation to the variable readonlyResult. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:40:14: Add a type annotation to the variable readonlyResult. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:40:57: Add a return type to the function expression. + export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); + ~~~~~~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:41:14: Add a type annotation to the variable mutableResult. + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9027 declarationEmitConstTypeParameterReadonly.ts:41:14: Add a type annotation to the variable mutableResult. +!!! related TS9030 declarationEmitConstTypeParameterReadonly.ts:41:59: Add a return type to the function expression. + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).symbols b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).symbols new file mode 100644 index 0000000000000..fe8b4b04bc819 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).symbols @@ -0,0 +1,161 @@ +//// [tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts] //// + +=== declarationEmitConstTypeParameterReadonly.ts === +declare function id(c: C): C; +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) +>c : Symbol(c, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 29)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) +>C : Symbol(C, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 20)) + +export const b = id({ A: { f: () => 1 } }); +>b : Symbol(b, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 21)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 1, 26)) + +export const annotatedFunction = id({ A: { f: (): number => 1 } }); +>annotatedFunction : Symbol(annotatedFunction, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 37)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 2, 42)) + +const leaf = { a: 1 } as const; +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) +>a : Symbol(a, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 14)) +>const : Symbol(const) + +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); +>twoRefs : Symbol(twoRefs, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>properties : Symbol(properties, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 27)) +>one : Symbol(one, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 41)) +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) +>two : Symbol(two, Decl(declarationEmitConstTypeParameterReadonly.ts, 5, 52)) +>leaf : Symbol(leaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 4, 5)) + +export const nested = id({ A: [{ f: () => 1 }] }); +>nested : Symbol(nested, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>A : Symbol(A, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 26)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 7, 32)) + +const tupleLeaf = [1, "a"] as const; +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) +>const : Symbol(const) + +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); +>twoTupleRefs : Symbol(twoTupleRefs, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 12)) +>id : Symbol(id, Decl(declarationEmitConstTypeParameterReadonly.ts, 0, 0)) +>properties : Symbol(properties, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 32)) +>one : Symbol(one, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 46)) +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) +>two : Symbol(two, Decl(declarationEmitConstTypeParameterReadonly.ts, 10, 62)) +>tupleLeaf : Symbol(tupleLeaf, Decl(declarationEmitConstTypeParameterReadonly.ts, 9, 5)) + +export const getterOnly = { +>getterOnly : Symbol(getterOnly, Decl(declarationEmitConstTypeParameterReadonly.ts, 12, 12)) + + get value(): number { +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 12, 27)) + + return 1; + }, +}; + +export const getterAndSetter = { +>getterAndSetter : Symbol(getterAndSetter, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 12)) + + get value(): number { +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 32), Decl(declarationEmitConstTypeParameterReadonly.ts, 21, 6)) + + return 1; + }, + set value(value: number) {}, +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 18, 32), Decl(declarationEmitConstTypeParameterReadonly.ts, 21, 6)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 22, 14)) + +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 40)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 31)) + +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; +>mutableInput : Symbol(mutableInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 83)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 39)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 57)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 26, 30)) + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +>mappedScalars : Symbol(mappedScalars, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 12)) +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>foo : Symbol(foo, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 44)) +>bar : Symbol(bar, Decl(declarationEmitConstTypeParameterReadonly.ts, 28, 56)) + +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +>readonlyParameter : Symbol(readonlyParameter, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 12)) +>readonlyInput : Symbol(readonlyInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 23, 2)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 48)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 29, 57)) + +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); +>mutableParameter : Symbol(mutableParameter, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 12)) +>mutableInput : Symbol(mutableInput, Decl(declarationEmitConstTypeParameterReadonly.ts, 25, 83)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 46)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 55)) + +declare function addReadonly(arg: { value: T }): { +>addReadonly : Symbol(addReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 72)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 38)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 44)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) + + value: { readonly [K in keyof T]: T[K] }; +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 59)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 33, 23)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 32, 29)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 33, 23)) + +}; +declare function removeReadonly(arg: { value: T }): { +>removeReadonly : Symbol(removeReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 34, 2)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>arg : Symbol(arg, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 41)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 47)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) + + value: { -readonly [K in keyof T]: T[K] }; +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 62)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 36, 24)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>T : Symbol(T, Decl(declarationEmitConstTypeParameterReadonly.ts, 35, 32)) +>K : Symbol(K, Decl(declarationEmitConstTypeParameterReadonly.ts, 36, 24)) + +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +>readonlyResult : Symbol(readonlyResult, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 12)) +>addReadonly : Symbol(addReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 30, 72)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 43)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 39, 52)) + +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); +>mutableResult : Symbol(mutableResult, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 12)) +>removeReadonly : Symbol(removeReadonly, Decl(declarationEmitConstTypeParameterReadonly.ts, 34, 2)) +>value : Symbol(value, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 45)) +>f : Symbol(f, Decl(declarationEmitConstTypeParameterReadonly.ts, 40, 54)) +>const : Symbol(const) + diff --git a/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).types b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).types new file mode 100644 index 0000000000000..ababa55394013 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/declarationEmitConstTypeParameterReadonly(isolateddeclarations=true).types @@ -0,0 +1,191 @@ +//// [tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts] //// + +=== declarationEmitConstTypeParameterReadonly.ts === +declare function id(c: C): C; +>id : (c: C) => C +>c : C + +export const b = id({ A: { f: () => 1 } }); +>b : { readonly A: { readonly f: () => 1; }; } +>id({ A: { f: () => 1 } }) : { readonly A: { readonly f: () => 1; }; } +>id : (c: C) => C +>{ A: { f: () => 1 } } : { A: { f: () => 1; }; } +>A : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const annotatedFunction = id({ A: { f: (): number => 1 } }); +>annotatedFunction : { readonly A: { readonly f: () => number; }; } +>id({ A: { f: (): number => 1 } }) : { readonly A: { readonly f: () => number; }; } +>id : (c: C) => C +>{ A: { f: (): number => 1 } } : { A: { f: () => number; }; } +>A : { f: () => number; } +>{ f: (): number => 1 } : { f: () => number; } +>f : () => number +>(): number => 1 : () => number +>1 : 1 + +const leaf = { a: 1 } as const; +>leaf : { readonly a: 1; } +>{ a: 1 } as const : { readonly a: 1; } +>{ a: 1 } : { readonly a: 1; } +>a : 1 +>1 : 1 + +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); +>twoRefs : { readonly properties: { readonly one: { readonly a: 1; }; readonly two: { readonly a: 1; }; }; } +>id({ properties: { one: leaf, two: leaf } }) : { readonly properties: { readonly one: { readonly a: 1; }; readonly two: { readonly a: 1; }; }; } +>id : (c: C) => C +>{ properties: { one: leaf, two: leaf } } : { properties: { one: { readonly a: 1; }; two: { readonly a: 1; }; }; } +>properties : { one: { readonly a: 1; }; two: { readonly a: 1; }; } +>{ one: leaf, two: leaf } : { one: { readonly a: 1; }; two: { readonly a: 1; }; } +>one : { readonly a: 1; } +>leaf : { readonly a: 1; } +>two : { readonly a: 1; } +>leaf : { readonly a: 1; } + +export const nested = id({ A: [{ f: () => 1 }] }); +>nested : { readonly A: readonly [{ readonly f: () => 1; }]; } +>id({ A: [{ f: () => 1 }] }) : { readonly A: readonly [{ readonly f: () => 1; }]; } +>id : (c: C) => C +>{ A: [{ f: () => 1 }] } : { A: [{ f: () => 1; }]; } +>A : [{ f: () => 1; }] +>[{ f: () => 1 }] : [{ f: () => 1; }] +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +const tupleLeaf = [1, "a"] as const; +>tupleLeaf : readonly [1, "a"] +>[1, "a"] as const : readonly [1, "a"] +>[1, "a"] : readonly [1, "a"] +>1 : 1 +>"a" : "a" + +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); +>twoTupleRefs : { readonly properties: { readonly one: readonly [1, "a"]; readonly two: readonly [1, "a"]; }; } +>id({ properties: { one: tupleLeaf, two: tupleLeaf } }) : { readonly properties: { readonly one: readonly [1, "a"]; readonly two: readonly [1, "a"]; }; } +>id : (c: C) => C +>{ properties: { one: tupleLeaf, two: tupleLeaf } } : { properties: { one: readonly [1, "a"]; two: readonly [1, "a"]; }; } +>properties : { one: readonly [1, "a"]; two: readonly [1, "a"]; } +>{ one: tupleLeaf, two: tupleLeaf } : { one: readonly [1, "a"]; two: readonly [1, "a"]; } +>one : readonly [1, "a"] +>tupleLeaf : readonly [1, "a"] +>two : readonly [1, "a"] +>tupleLeaf : readonly [1, "a"] + +export const getterOnly = { +>getterOnly : { readonly value: number; } +>{ get value(): number { return 1; },} : { readonly value: number; } + + get value(): number { +>value : number + + return 1; +>1 : 1 + + }, +}; + +export const getterAndSetter = { +>getterAndSetter : { value: number; } +>{ get value(): number { return 1; }, set value(value: number) {},} : { value: number; } + + get value(): number { +>value : number + + return 1; +>1 : 1 + + }, + set value(value: number) {}, +>value : number +>value : number + +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>arg : { readonly [K in keyof T]: T[K]; } + +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; +>mutableInput : (arg: { -readonly [K in keyof T]: T[K]; }) => T +>arg : { -readonly [K in keyof T]: T[K]; } + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +>mappedScalars : { foo: "str"; bar: 42; } +>readonlyInput({ foo: "str", bar: 42 }) : { foo: "str"; bar: 42; } +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>{ foo: "str", bar: 42 } : { foo: "str"; bar: 42; } +>foo : "str" +>"str" : "str" +>bar : 42 +>42 : 42 + +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +>readonlyParameter : { value: { readonly f: () => 1; }; } +>readonlyInput({ value: { f: () => 1 } }) : { value: { readonly f: () => 1; }; } +>readonlyInput : (arg: { readonly [K in keyof T]: T[K]; }) => T +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); +>mutableParameter : { readonly value: { readonly f: () => 1; }; } +>mutableInput({ value: { f: () => 1 } }) : { readonly value: { readonly f: () => 1; }; } +>mutableInput : (arg: { -readonly [K in keyof T]: T[K]; }) => T +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +declare function addReadonly(arg: { value: T }): { +>addReadonly : (arg: { value: T; }) => { value: { readonly [K in keyof T]: T[K]; }; } +>arg : { value: T; } +>value : T + + value: { readonly [K in keyof T]: T[K] }; +>value : { readonly [K in keyof T]: T[K]; } + +}; +declare function removeReadonly(arg: { value: T }): { +>removeReadonly : (arg: { value: T; }) => { value: { -readonly [K in keyof T]: T[K]; }; } +>arg : { value: T; } +>value : T + + value: { -readonly [K in keyof T]: T[K] }; +>value : { -readonly [K in keyof T]: T[K]; } + +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +>readonlyResult : { value: { readonly f: () => 1; }; } +>addReadonly({ value: { f: () => 1 } }) : { value: { readonly f: () => 1; }; } +>addReadonly : (arg: { value: T; }) => { value: { readonly [K in keyof T]: T[K]; }; } +>{ value: { f: () => 1 } } : { value: { f: () => 1; }; } +>value : { f: () => 1; } +>{ f: () => 1 } : { f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const); +>mutableResult : { value: { f: () => 1; }; } +>removeReadonly({ value: { f: () => 1 } } as const) : { value: { f: () => 1; }; } +>removeReadonly : (arg: { value: T; }) => { value: { -readonly [K in keyof T]: T[K]; }; } +>{ value: { f: () => 1 } } as const : { readonly value: { readonly f: () => 1; }; } +>{ value: { f: () => 1 } } : { readonly value: { readonly f: () => 1; }; } +>value : { readonly f: () => 1; } +>{ f: () => 1 } : { readonly f: () => 1; } +>f : () => 1 +>() => 1 : () => 1 +>1 : 1 + diff --git a/tsc/testdata/tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts b/tsc/testdata/tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts new file mode 100644 index 0000000000000..4c93d743cdfb5 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/declarationEmitConstTypeParameterReadonly.ts @@ -0,0 +1,45 @@ +// @declaration: true +// @emitDeclarationOnly: true +// @isolatedDeclarations: false, true + +declare function id(c: C): C; +export const b = id({ A: { f: () => 1 } }); +export const annotatedFunction = id({ A: { f: (): number => 1 } }); + +const leaf = { a: 1 } as const; +export const twoRefs = id({ properties: { one: leaf, two: leaf } }); + +export const nested = id({ A: [{ f: () => 1 }] }); + +const tupleLeaf = [1, "a"] as const; +export const twoTupleRefs = id({ properties: { one: tupleLeaf, two: tupleLeaf } }); + +export const getterOnly = { + get value(): number { + return 1; + }, +}; + +export const getterAndSetter = { + get value(): number { + return 1; + }, + set value(value: number) {}, +}; + +declare function readonlyInput(arg: { readonly [K in keyof T]: T[K] }): T; +declare function mutableInput(arg: { -readonly [K in keyof T]: T[K] }): T; + +export const mappedScalars = readonlyInput({ foo: "str", bar: 42 }); +export const readonlyParameter = readonlyInput({ value: { f: () => 1 } }); +export const mutableParameter = mutableInput({ value: { f: () => 1 } }); + +declare function addReadonly(arg: { value: T }): { + value: { readonly [K in keyof T]: T[K] }; +}; +declare function removeReadonly(arg: { value: T }): { + value: { -readonly [K in keyof T]: T[K] }; +}; + +export const readonlyResult = addReadonly({ value: { f: () => 1 } }); +export const mutableResult = removeReadonly({ value: { f: () => 1 } } as const);